Swagger:https://swagger.io/ 在前后台分离的开发模式中,减小接口定义沟通成本,方便开发过程中测试,自动生成接口文档
作为Java服务端的大一统框架Spring,将Swagger规范纳入自身的标准,建立了Spring-swagger项目,后面改成了现在的Springfox
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> 配置类:没有配置类Swagger无法运行 package demo.config; import io.swagger.annotations.Api; 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.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /* * Author : zfk * Date : 14:46 */ @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2 .apiInfo(apiInfo()) // 用于定义api文档汇总信息,即下面的配置 .select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) // 指定controller类,即加了Api注解的类 .paths(PathSelectors.any()) // 选择所有controller .build(); } //构建api文档的相关信息 private ApiInfo apiInfo(){ return new ApiInfoBuilder() // 页面标题 .title("JPATest API Doc") // 描述 .description("This is a restful api document of nc server.") //版本号 .version("1.0") .build(); } }Docket是核心类,这里用流式编程,还有很多属性
其中.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) 中指定controller为加了Api注解的类,其实RequestHandlerSelectors还有其他的配置
扫描包.apis(RequestHandlerSelectors.basePackage("com.demo.controller"))
Controller配置首先我们在Docket配置Controller是所有加上了@Api注解的类(也可以设置为扫描包) 接口上需要设置@ApiOperation注解
一个UserController:因为这里是讲Swagger,就不贴Springboot的mvc代码
package demo.controller; import demo.entity.User; import demo.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.util.Optional; /** * Author : zfk * Date : 14:40 */ @RestController @Api("UserController") public class UserController { @Autowired private UserService service; @GetMapping("/findById/{id}") @ApiOperation(value = "findById") public User findById(@PathVariable("id") String id){ Optional<User> user = service.findById(id); return user.get(); } @GetMapping("/finByName/{name}") @ApiOperation(value = "findByName") public User findByName(@PathVariable("name")String name){ User user = service.findByName(name); return user; } } 运行Springboot后,访问地址http://localhost:8080/swagger-ui.html界面:
测试接口虽然Swagger很强,但有点丑,而且无法便捷导出接口文档,有需求就有市场,github就有对应的插件knife4j
knife4j完全遵循了springfox-swagger中的使用方式,并在此基础上做了增强功能
knife4j官方文档
当然,需要更细节的配置
@Bean(value = "defaultApi2") public Docket defaultApi2() { Docket docket=new Docket(DocumentationType.SWAGGER_2) .apiInfo(new ApiInfoBuilder() //.title("swagger-bootstrap-ui-demo RESTful APIs") .description("# swagger-bootstrap-ui-demo RESTful APIs") .termsOfServiceUrl("http://www.xx.com/") .contact("xx@qq.com") .version("1.0") .build()) //分组名称 .groupName("2.X版本") .select() //这里指定Controller扫描包路径 .apis(RequestHandlerSelectors.basePackage("com.htphy.wx.module.dev.controller")) .paths(PathSelectors.any()) .build(); return docket; } 启动Springboot,Knife4j网址为http://localhost:8080/doc.html里面的下载功能很强大
还有很多优化的地方,具体看官方文档
