先后端分离后,维护接口文档基本上是必不可少的工做。一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就能够上线了。固然这是一种很是理想的状态,实际开发中却不多遇到这样的状况,接口老是在不断的变化之中,有变化就要去维护,作过的小伙伴都知道这件事有多么头大!还好,有一些工具能够减轻咱们的工做量,Swagger2就是其中之一,至于其余相似功能可是却收费的软件,这里就不作过多介绍了。本文主要和大伙来聊下在Spring Boot中如何整合Swagger2。html
固然,首先是建立一个Spring Boot项目,加入web依赖,建立成功后,加入两个Swagger2相关的依赖,完整的依赖以下:前端
<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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Swagger2的配置也是比较容易的,在项目建立成功以后,只须要开发者本身提供一个Docket的Bean便可,以下:web
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .pathMapping("/") .select() .apis(RequestHandlerSelectors.basePackage("com.nvn.controller")) .paths(PathSelectors.any()) .build().apiInfo(new ApiInfoBuilder() .title("SpringBoot整合Swagger") .description("SpringBoot整合Swagger,详细信息......") .version("9.0") .contact(new Contact("啊啊啊啊","blog.csdn.net","aaa@gmail.com")) .license("The Apache License") .licenseUrl("http://www.baidu.com") .build()); } }
这里提供一个配置类,首先经过@EnableSwagger2注解启用Swagger2,而后配置一个Docket Bean,这个Bean中,配置映射路径和要扫描的接口的位置,在apiInfo中,主要配置一下Swagger2文档网站的信息,例如网站的title,网站的描述,联系人的信息,使用的协议等等。 spring
如此,Swagger2就算配置成功了,很是方便。 后端
此时启动项目,输入http://localhost:8080/swagger-ui.html,可以看到以下页面,说明已经配置成功了: api
接下来就是建立接口了,Swagger2相关的注解其实并很少,并且很容易懂,下面我来分别向小伙伴们举例说明:app
@RestController @Api(tags = "用户管理相关接口") @RequestMapping("/user") public class UserController { @PostMapping("/") @ApiOperation("添加用户的接口") @ApiImplicitParams({ @ApiImplicitParam(name = "username", value = "用户名", defaultValue = "李四"), @ApiImplicitParam(name = "address", value = "用户地址", defaultValue = "深圳", required = true) } ) public RespBean addUser(String username, @RequestParam(required = true) String address) { return new RespBean(); } @GetMapping("/") @ApiOperation("根据id查询用户的接口") @ApiImplicitParam(name = "id", value = "用户id", defaultValue = "99", required = true) public User getUserById(@PathVariable Integer id) { User user = new User(); user.setId(id); return user; } @PutMapping("/{id}") @ApiOperation("根据id更新用户的接口") public User updateUserById(@RequestBody User user) { return user; } }
这里边涉及到多个API,我来向小伙伴们分别说明:框架
@ApiModel public class User { @ApiModelProperty(value = "用户id") private Integer id; @ApiModelProperty(value = "用户名") private String username; @ApiModelProperty(value = "用户地址") private String address; //getter/setter }
好了,通过如上配置以后,接下来,刷新刚刚打开的页面,能够看到以下效果: 前后端分离
能够看到,全部的接口这里都列出来了,包括接口请求方式,接口地址以及接口的名字等,点开一个接口,能够看到以下信息: ide
能够看到,接口的参数,参数要求,参数默认值等等通通都展现出来了,参数类型下的query表示参数以key/value的形式传递,点击右上角的Try it out,就能够进行接口测试:
点击Execute按钮,表示发送请求进行测试。测试结果会展现在下面的Response中。
小伙伴们注意,参数类型下面的query表示参数以key/value的形式传递,这里的值也多是body,body表示参数以请求体的方式传递,例如上文的更新接口,以下:
固然还有一种可能就是这里的参数为path,表示参数放在路径中传递,例如根据id查询用户的接口:
固然,除了这些以外,还有一些响应值的注解,都比较简单,小伙伴能够本身摸索下。
若是咱们的Spring Boot项目中集成了Spring Security,那么若是不作额外配置,Swagger2文档可能会被拦截,此时只须要在Spring Security的配置类中重写configure方法,添加以下过滤便可:
@Override public void configure(WebSecurity web) throws Exception { web.ignoring() .antMatchers("/swagger-ui.html") .antMatchers("/v2/**") .antMatchers("/swagger-resources/**"); }
如此以后,Swagger2文件就不须要认证就能访问了。不知道小伙伴们有没有看懂呢?有问题欢迎留言讨论。
关注公众号【江南一点雨】,专一于 Spring Boot+微服务以及先后端分离等全栈技术,按期视频教程分享,关注后回复 Java ,领取松哥为你精心准备的 Java 干货!