文章目录
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* Swagger的配置类
* @author 陈加兵
*
*/
@Configuration
public class SwaggerConfig {
/**
* 创建用户API文档
* @return
*/
@Bean
public Docket createRestUserApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("user")
.apiInfo(apiInfo()) //api的信息
.select()
.apis(RequestHandlerSelectors
.basePackage("cn.tedu.mycat.controller")) //添加包扫描
.paths(PathSelectors.any()).build();
}
/**
* 创建API信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("api文档的标题") //标题
.description("api文档的描述") //描述
.contact( //添加开发者的一些信息
new Contact("爱撒谎的男孩", "https://chenjiabing666.github.io",
"18796327106@163.com")).version("1.0").build();
}
}
@EnableSwagger2
http://ip/projectName/swagger-ui.html
:html的页面http://localhost:8080/demo/v2/api-docs
:这个是json数据的页面属性名称 | 备注 | 默认值 |
---|---|---|
value | url的路径值 | |
tags | 如果设置这个值、value的值会被覆盖 | |
description | 对api资源的描述 | |
basePath | 基本路径可以不配置 | |
position | 如果配置多个Api 想改变显示的顺序位置 | |
produces | For example, “application/json, application/xml” | |
consumes | For example, “application/json, application/xml” | |
protocols | Possible values: http, https, ws, wss. | |
authorizations | 高级特性认证时配置 | |
hidden | 配置为true 将在文档中隐藏 |
value | url的路径值 | |
---|---|---|
tags | 如果设置这个值、value的值会被覆盖 | |
notes | 对api资源的描述 | |
response | 返回的对象,在文档中点击Model可以获取该配置的内容 | |
responseContainer | 这些对象是有效的 “List”, “Set” or “Map”.,其他无效 | |
responseReference | 可以不配置 | |
httpMethod | 可以接受 “GET”, “HEAD”, “POST”, “PUT”, “DELETE”, “OPTIONS” and “PATCH” | |
position | 如果配置多个Api 想改变显示的顺序位置 | |
produces | 同 Api中的定义 | |
consumes | 同 Api中的定义 | |
protocols | 同 Api中的定义 | |
authorizations | 同 Api中的定义 | |
hidden | 是否隐藏,true 或者false ,这个可以隐藏后台接口 | |
code | http的状态码 默认 200 | |
extensions | 扩展属性 |
@ApiImplicitParams
注解中,也可以单独使用,说明一个请求参数的各个方面 name
:属性的字段名称,相当于form表单中的name,这个就是入参的字段dataType
:参数的类型,标识,字符串value
:该参数的描述required
:是否必填,布尔值defaultValue
:缺省值,会在文档中缺省填入,这样更方面造数据,不需要调用接口的去填值了paramType
:指定参数的入参数方式(也就是请求参数的位置),其中有四种常用的,如下:query
path
body
form
query
:必须要和入参的字段一样,也可以使用@RequestParam()
指定path
:用于Restful的风格的url,请求的参数写在路径上,如下:@ApiOperation(value="根据用户Id获取用户信息",response=User.class,hidden=false)
@ApiImplicitParams({
@ApiImplicitParam(paramType = "path", name = "id", dataType="Integer", required = false, value = "用户的id", defaultValue = "1")
})
@GetMapping("/user/get/{id}")
public Object getUser(@PathVariable("id")Integer id) {
return new User(id, "陈加兵");
}
body
:以流的形式提交 仅支持POST form
:以表单的形式提交<!-- swagger自动生成文档依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger-version}</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger-version}</version>
</dependency>
<!--导出到markdown文件的依赖 -->
<dependency>
<groupId>io.github.swagger2markup</groupId>
<artifactId>swagger2markup</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>ch.netzwerg</groupId>
<artifactId>paleo-core</artifactId>
<version>0.11.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/paleo-core-0.11.0.jar</systemPath>
</dependency>
<dependency>
<groupId>nl.jworks.markdown_to_asciidoc</groupId>
<artifactId>markdown_to_asciidoc</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/markdown_to_asciidoc-1.0.jar</systemPath>
</dependency>
paleo-core
和markdown_to_asciidoc
,但是这两个依赖使用maven不能自动导入,只能使用本地jar的方式了import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import io.github.swagger2markup.GroupBy;
import io.github.swagger2markup.Language;
import io.github.swagger2markup.Swagger2MarkupConfig;
import io.github.swagger2markup.Swagger2MarkupConverter;
import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder;
import io.github.swagger2markup.markup.builder.MarkupLanguage;
/**
* 生成Markdown文件的类
*/
public class SwaggerTest {
public static void main(String[] args) throws Exception {
Path outputFile = Paths.get("build/swagger"); //指定生成的目录和文件的名称swagger.md
Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
.withMarkupLanguage(MarkupLanguage.MARKDOWN)
.withOutputLanguage(Language.ZH)
.withPathsGroupedBy(GroupBy.TAGS)
.withGeneratedExamples()
.withoutInlineSchema()
.build();
Swagger2MarkupConverter converter = Swagger2MarkupConverter.from(new URL("http://localhost:8080/demo/v2/api-docs")) //url是可以访问的在线json数据的url
.withConfig(config)
.build();
converter.toFile(outputFile);
}
}