在spring boot下建议使用:
http://localhost:8080/swagger-ui.html
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.1.RELEASE</version>
</dependency
添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
1、swagger配置类
2
public class SwaggerConfig {
public static final String SWAGGER_SCAN_BASE_PACKAGE = "top.zlcxy.boot.examples.web";
public static final String VERSION = "1.0.0";
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))//api接口包扫描路径
.paths(PathSelectors.any())//可以根据url路径设置哪些请求加入文档,忽略哪些请求
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger2 接口文档示例")//设置文档的标题
.description("更多内容请关注:http://www.zlcxy.top")//设置文档的描述
.version(VERSION)//设置文档的版本信息
.contact(new Contact("zlcxy", "http://www.zlcxy.top", ""))//设置文档的联系方式
.termsOfServiceUrl("www.zlcxy.top")//设置文档的License信息
.build();
}
}
2、注解使用
//ApiOperation 对接口的描述
public List<User> getUserList() {
List<User> r = new ArrayList<User>(users.values());
return r;
}
// ApiResponses 返回结果的描述
//ApiImplicitParam 接口参数的描述
})
public User getUser( Integer id) {
return users.get(id);
}
// ApiImplicitParams 多个参数的描述,paramType 有五个可选值 :path, query, body, header, form
@ApiImplicitParam(name = , ,value = ,paramType = ,dataType = )
})
public void updateUserName( Integer id, String userName){
User u = users.get(id);
u.setName(userName);
}
//ApiParam 参数描述 或者使用ApiImplicitParams注解
"创建用户-传递简单对象", notes="传递简单对象",produces = "application/json") (value=
"/users-1", method= RequestMethod.POST) (value=
public Map postUser( String userName, ("地址") (required = false) String address) {
User user = new User();
user.setId(Math.round(10));
user.setName(userName);
user.setAddress(address);
users.put(user.getId(), user);
return ImmutableMap.of("user",user);
}
//ModelAttribute 传递复杂对象描述
public Map postUser2( User user) {
users.put(user.getId(),user);
return ImmutableMap.of("user",user);
}
//ApiModel 实体对象的描述
//ApiModelProperty 实体对象属性的描述
public class User {
private Integer id;
private String name;
private String address;
private int age;
private int sex;
.......
}
3、配置
application.properties中配置参数
#改变请求的根路径
springfox.documentation.swagger.v2.host = api.zlcxy.top
#配置获取api docs json数据的请求路径 ,默认为/v2/api-docs
springfox.documentation.swagger.v2.path = /api
4、springfox-staticdocs 生成静态文档
maven引入jar包
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-staticdocs</artifactId>
<version>2.2.2</version>
<scope>test</scope>
</dependency>
生成json文件
public class Swagger2MarkupTest {
private WebApplicationContext context;
private MockMvc mockMvc;
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
public void createSpringfoxSwaggerJson() throws Exception {
String outputDir = "src/docs/json"; //将api-docs的json数据写入文件
MvcResult mvcResult = this.mockMvc.perform(get("/v2/api-docs")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
MockHttpServletResponse response = mvcResult.getResponse();
String swaggerJson = response.getContentAsString();
Files.createDirectories(Paths.get(outputDir));
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputDir, "swagger.json"), StandardCharsets.UTF_8)) {
writer.write(swaggerJson);
}
}
}
配置Maven Plugin
swagger2markup-maven-plugin,该插件将json文件转为asciidoc
asciidoctor-maven-plugin, 该插件将asciidoc转为html/pdf
#执行Maven命令 : mvn swagger2markup:convertSwagger2markup process-resources
<plugins>
<plugin>
<groupId>io.github.swagger2markup</groupId>
<artifactId>swagger2markup-maven-plugin</artifactId>
<version>${swagger2markup.plugin.version}</version>
<dependencies>
<dependency>
<groupId>io.github.swagger2markup</groupId>
<artifactId>swagger2markup-import-files-ext</artifactId>
<version>${swagger2markup.extension.version}</version>
</dependency>
<dependency>
<groupId>io.github.swagger2markup</groupId>
<artifactId>swagger2markup</artifactId>
<version>${swagger2markup.version}</version>
</dependency>
</dependencies>
<configuration>
<swaggerInput>${swagger.input}</swaggerInput>
<outputDir>${generated.asciidoc.directory}</outputDir>
<config>
<!--设置输出文件的语言:ASCIIDOC, MARKDOWN, CONFLUENCE_MARKUP-->
<swagger2markup.markupLanguage>ASCIIDOC</swagger2markup.markupLanguage>
<!--设置目录的展现方式-->
<swagger2markup.pathsGroupedBy>TAGS</swagger2markup.pathsGroupedBy>
<!--扩展Overview的内容,可以增加一些自定义的内容-->
<!--<swagger2markup.extensions.dynamicOverview.contentPath>${project.basedir}/src/docs/asciidoc/extensions/overview</swagger2markup.extensions.dynamicOverview.contentPath>
<swagger2markup.extensions.dynamicDefinitions.contentPath>${project.basedir}/src/docs/asciidoc/extensions/definitions</swagger2markup.extensions.dynamicDefinitions.contentPath>
<swagger2markup.extensions.dynamicPaths.contentPath>${project.basedir}/src/docs/asciidoc/extensions/paths</swagger2markup.extensions.dynamicPaths.contentPath>
<swagger2markup.extensions.dynamicSecurity.contentPath>${project.basedir}src/docs/asciidoc/extensions/security</swagger2markup.extensions.dynamicSecurity.contentPath>-->
</config>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>convertSwagger2markup</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.3</version>
<dependencies>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj-pdf</artifactId>
<version>1.5.0-alpha.11</version>
</dependency>
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-complete</artifactId>
<version>${jruby.version}</version>
</dependency>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj</artifactId>
<version>${asciidoctorj.version}</version>
</dependency>
</dependencies>
<configuration>
<!--默认指向 ${basedir}/src/main/asciidoc-->
<sourceDirectory>${asciidoctor.input.directory}</sourceDirectory>
<!--an override to process a single source file; 默认指向 ${sourceDirectory} 中的所有文件-->
<!--<sourceDocumentName>index.adoc</sourceDocumentName>-->
<attributes>
<doctype>book</doctype>
<toc>left</toc>
<toclevels>3</toclevels>
<numbered></numbered>
<hardbreaks></hardbreaks>
<sectlinks></sectlinks>
<sectanchors></sectanchors>
<generated>${generated.asciidoc.directory}</generated>
</attributes>
</configuration>
<executions>
<execution>
<id>output-html</id>
<phase>generate-resources</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<backend>html5</backend>
<outputDirectory>${asciidoctor.html.output.directory}</outputDirectory>
</configuration>
</execution>
<!-- 生成PDF -->
<!--<execution>
<id>output-pdf</id>
<phase>generate-resources</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
<configuration>
<backend>pdf</backend>
<outputDirectory>${asciidoctor.pdf.output.directory}</outputDirectory>
</configuration>
</execution>-->
</executions>
</plugin>
</plugins>
更多文章请关注公众号,谢谢
公众号回复“springfox”, 获取springfox文档地址
联系方式:13128600812
本文分享自微信公众号 - IT技术屋(zhanglcxyworkshop)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
来源:oschina
链接:https://my.oschina.net/u/1020235/blog/4478642