spring boot下建议使用:
https://github.com/SpringForAll/spring-boot-starter-swagger
<dependency>
<groupId>com.spring4allgroupId>
<artifactId>swagger-spring-boot-starterartifactId>
<version>1.7.1.RELEASEversion>
dependency>
按以下步骤配置,项目启动后访问:
http://localhost:8080/swagger-ui.html
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.2.2version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.2.2version>
dependency>
@Configuration
@EnableSwagger2
public class Swagger2 {
public static final String SWAGGER_SCAN_BASE_PACKAGE = "abc.boot.examples.web";
public static final String VERSION = "1.0.0";
@Bean
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.abc.com")//设置文档的描述->1.Overview
.version(VERSION)//设置文档的版本信息-> 1.1 Version information
.contact(new Contact("ABC Boot", "http://www.abc.comt", ""))//设置文档的联系方式->1.2 Contact information
.termsOfServiceUrl("www.abc.com")//设置文档的License信息->1.3 License information
.build();
}
}
@ApiOperation
@ApiOperation(value="获取用户列表", notes="获取所有用户列表",produces = "application/json")
@RequestMapping(value="/users", method= RequestMethod.GET)
public List<User> getUserList() {
List<User> r = new ArrayList<User>(users.values());
return r;
}
@ApiResponses
@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息",produces = "application/json")
// ApiResponses 增加返回结果的描述
@ApiResponses(value = {@ApiResponse(code = 405,message = "Invalid input",response = Integer.class)}) (1)
@ApiImplicitParam(name = "id",value = "用户ID",dataType = "int",paramType = "path") (2)
@RequestMapping(value="/users/{id}", method= RequestMethod.GET)
public User getUser(@PathVariable Integer id) {
return users.get(id);
}
@ApiImplicitParams
@ApiOperation(value="更新用户名称", notes="更新指定用户的名称")
@RequestMapping(value="/users/{id}", method= RequestMethod.POST)
@ApiImplicitParams({ (1)
@ApiImplicitParam(name = "id",value = "用户ID",paramType = "path",dataType = "int"), (2)
@ApiImplicitParam(name = "userName",value = "用户名称",paramType = "form",dataType = "string")
})
public void updateUserName(@PathVariable Integer id,@RequestParam String userName){
User u = users.get(id);
u.setName(userName);
}
paramType 有五个可选值 : path, query, body, header, form
@ApiParam
@ApiOperation(value="创建用户-传递简单对象", notes="传递简单对象",produces = "application/json")
@RequestMapping(value="/users-1", method= RequestMethod.POST)
//可以不加ApiParam注解,需要给参数添加描述时可以使用这个注解,或者使用ApiImplicitParams注解 (1)
public Map postUser(@RequestParam String userName,@ApiParam("地址") @RequestParam(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);
}
ApiImplicitParam 与 ApiParam 的区别
ApiImplicitParam: This is the only way to define parameters when using Servlets or other non-JAX-RS environments.
对Servlets或者非 JAX-RS的环境,只能使用 ApiImplicitParam。在使用上,ApiImplicitParam比ApiParam具有更少的代码侵入性,只要写在方法上就可以了,但是需要提供具体的属性才能配合swagger ui解析使用。
ApiParam只需要较少的属性,与swagger ui配合更好。传递复杂对象 By ModelAttribute
@ApiOperation(value="创建用户-传递复杂对象", notes="传递复杂对象DTO, url参数拼接",produces = "application/json")
@RequestMapping(value="/users-2", method= RequestMethod.POST)
//传递对象推荐使用ModelAttribute注解
public Map postUser2(@ModelAttribute User user) { (1)
users.put(user.getId(),user);
return ImmutableMap.of("user",user);
}
@ApiModel
@ApiModel(value = "User", description = "用户对象")
public class User {
@ApiModelProperty(value = "ID")
private Integer id;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "地址")
private String address;
@ApiModelProperty(value = "年龄",access = "hidden")
private int age;
@ApiModelProperty(value = "性别")
private int sex;
.......
}
传递复杂对象 By RequestBody
@ApiOperation(value="创建用户-传递复杂对象", notes="传递复杂对象DTO,json格式传递数据",produces = "application/json")
@RequestMapping(value="/users-3", method= RequestMethod.POST)
//json格式传递对象使用RequestBody注解
public User postUser3(@RequestBody User user) {
users.put(user.getId(),user);
return user;
}
PathVariable
@ApiOperation(value="删除用户- PathVariable", notes="根据url的id来指定删除对象")
@RequestMapping(value="/users/{id}", method = RequestMethod.DELETE)
public void deleteUser(@PathVariable Integer id) { (1)
users.remove(id);
}
数组的描述
@ApiOperation(value="删除用户-传递数组", notes="删除对象,传递数组")
@RequestMapping(value="/users/deleteByIds", method = RequestMethod.DELETE)
public void deleteUser(@ApiParam("用户ID数组") @RequestParam Integer[] ids) { (1)
for (int id:ids){
users.remove(id);
}
}
在application.properties中加入以下配置,用于设置测试请求的host,默认在swagger ui上做请求测试时都是以/users/1为路径发送请求。
如果需要改变请求的根路径,就需要配置这个参数:springfox.documentation.swagger.v2.host = yourapp.abc.com
配置获取api docs json数据的请求路径 ,默认为/v2/api-docs:springfox.documentation.swagger.v2.path = /api
springfox
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-staticdocsartifactId>
<version>2.2.2version>
<scope>testscope>
dependency>
编写Junit测试,这样在测试环节就可以将api-docs的json数据写入文档,便于下一步生成asciidoc文件。
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoBootApplication.class)
public class Swagger2MarkupTest {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
@Test
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);
}
}
}
配置以下两个插件:
swagger2markup-maven-plugin,该插件将json文件转为asciidoc
asciidoctor-maven-plugin, 该插件将asciidoc转为html/pdf
执行Maven命令 : mvn swagger2markup:convertSwagger2markup process-resources
生成的html文档存储在src\main\resources\META-INF\resources\docs目录下。
启动DemoBootApplication,直接访问http://localhost:8080/docs/index.html。
<pluginRepositories>
<pluginRepository>
<id>jcenter-snapshotsid>
<name>jcentername>
<url>http://oss.jfrog.org/artifactory/oss-snapshot-local/url>
pluginRepository>
<pluginRepository>
<snapshots>
<enabled>falseenabled>
snapshots>
<id>jcenter-releasesid>
<name>jcentername>
<url>http://jcenter.bintray.comurl>
pluginRepository>
pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>io.github.swagger2markupgroupId>
<artifactId>swagger2markup-maven-pluginartifactId>
<version>${swagger2markup.plugin.version}version>
<dependencies>
<dependency>
<groupId>io.github.swagger2markupgroupId>
<artifactId>swagger2markup-import-files-extartifactId>
<version>${swagger2markup.extension.version}version>
dependency>
<dependency>
<groupId>io.github.swagger2markupgroupId>
<artifactId>swagger2markupartifactId>
<version>${swagger2markup.version}version>
dependency>
dependencies>
<configuration>
<swaggerInput>${swagger.input}swaggerInput>
<outputDir>${generated.asciidoc.directory}outputDir>
<config>
<swagger2markup.markupLanguage>ASCIIDOCswagger2markup.markupLanguage>
<swagger2markup.pathsGroupedBy>TAGSswagger2markup.pathsGroupedBy>
config>
configuration>
<executions>
<execution>
<phase>generate-sourcesphase>
<goals>
<goal>convertSwagger2markupgoal>
goals>
execution>
executions>
plugin>
<plugin>
<groupId>org.asciidoctorgroupId>
<artifactId>asciidoctor-maven-pluginartifactId>
<version>1.5.3version>
<dependencies>
<dependency>
<groupId>org.asciidoctorgroupId>
<artifactId>asciidoctorj-pdfartifactId>
<version>1.5.0-alpha.11version>
dependency>
<dependency>
<groupId>org.jrubygroupId>
<artifactId>jruby-completeartifactId>
<version>${jruby.version}version>
dependency>
<dependency>
<groupId>org.asciidoctorgroupId>
<artifactId>asciidoctorjartifactId>
<version>${asciidoctorj.version}version>
dependency>
dependencies>
<configuration>
<sourceDirectory>${asciidoctor.input.directory}sourceDirectory>
<attributes>
<doctype>bookdoctype>
<toc>lefttoc>
<toclevels>3toclevels>
<numbered>numbered>
<hardbreaks>hardbreaks>
<sectlinks>sectlinks>
<sectanchors>sectanchors>
<generated>${generated.asciidoc.directory}generated>
attributes>
configuration>
<executions>
<execution>
<id>output-htmlid>
<phase>generate-resourcesphase>
<goals>
<goal>process-asciidocgoal>
goals>
<configuration>
<backend>html5backend>
<outputDirectory>${asciidoctor.html.output.directory}outputDirectory>
configuration>
execution>
executions>
plugin>
plugins>
build>
swagger-ui是通过获取接口的json数据渲染页面的,即通过swagger的注解将生成接口的描述服务,默认地址为==/v2/api-docs==,如果需要改变这个请求地址,可以在properties中配置springfox.documentation.swagger.v2.path。
默认请求都是以 / 根路径开始,如果我们的应用不是部署在根路径,比如以==/platform==部署,则可以通过一下方式设置请求的统一前缀。
@Bean
public Docket createV1RestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
.paths(PathSelectors.any())
.build()
.pathMapping("/platform"); // 在这里可以设置请求的统一前缀
}
接口文档中的 1.4和 1.5 则通过以下方式生成:
1.4 URI scheme
// 可以通过在properties中设置 springfox.documentation.swagger.v2.host属性
Host : localhost
// 待确认
BasePath : /
该Host也是swagger-ui发送测试请求的Host, 通常我们会将将接口文档部署在测试服务器,这样就需要设置Host,
否则请求都是通过localhost发送,请求不到测试服务器的接口。
1.5 Tags
@Api(value = “/v1/users”,tags = “Users”,description = “用户接口V1”)
tags由Api注解的tags标签设置,如果不设置,则以类名作为tag
通过ApiModelProperty注解的example属性设置响应对象的示例:
@ApiModelProperty(value = "ID",example = "1")
private Integer id;
@ApiModelProperty(value = "姓名",example = "Admin")
private String name;
@ApiModelProperty(value = "地址",example = "171")
private String address;
@ApiModelProperty(value = "年龄",access = "hidden",example = "20")
private int age;
@ApiModelProperty(value = "性别",example = "1")
private int sex;
@ApiModelProperty(value = "生日",example = "2000-10-22")
链接
springfox文档
http://www.jianshu.com/p/b730b969b6a2