最近 SpringFox 3.0.0 公布了,间隔上一次大版本2.9.2足足有2年多工夫了。可能看到这个名字,很多读者会有点生疏。然而,只有给大家看一下这两个依赖,你就晓得了!
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> <scope>compile</scope></dependency><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> <scope>compile</scope></dependency>
当咱们在应用Spring MVC写接口的时候,为了生成API文档,为了不便整合Swagger,都是用这个SpringFox的这套封装。然而,自从2.9.2版本更新之后,就始终没有什么动静,也没有更上Spring Boot的大潮流,有一段时间还始终都是写个配置类来为我的项目增加文档配置的。为此,之前就造了这么个轮子:
- https://github.com/SpringForAll/spring-boot-starter-swagger
也没什么难度,就是造的早,所以失去了不少Star。当初SpringFox出了一个starter,看了一下性能,尽管还不完满,但相较于之前咱们本人的轮子来说还是好蛮多的。来看看这个版本有些什么亮点:
- Spring 5,Webflux 反对(仅申请映射反对,尚不反对性能端点)
- Spring Integration 反对
- Spring Boot 反对 springfox-boot-starter 依赖性(零配置,主动配置反对)
- 具备主动实现性能的文档化配置属性
- 更好的标准兼容性
- 反对 OpenApi 3.0.3
- 简直零依赖性(惟一须要的库是 spring-plugin、pswagger-core)
- 现有的 swagger2 正文将持续无效,并丰盛 open API 3.0 标准
对于这次的更新,我感觉比较突出的几点:Webflux的反对,目前的轮子就没有做到;对OpenApi 3的反对;以及对Swagger 2的兼容(能够比拟不便的做降级了)。
上手尝鲜
说那么多,不如来一发程序试验下更间接!
第一步:创立一个Spring Boot我的项目,这里不开展,不会的看以前的教程:疾速入门
第二步:pom.xml
中增加依赖:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version><dependency>
当初简洁了不少,一个依赖搞定!
第三步:利用主类减少注解@EnableOpenApi
。
@EnableOpenApi@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}
第四步:配置一些接口例子,比方:
@Api(tags="用户治理")@RestControllerpublic class UserController { @ApiOperation("创立用户") @PostMapping("/users") public User create(@RequestBody @Valid User user) { return user; } @ApiOperation("用户详情") @GetMapping("/users/{id}") public User findById(@PathVariable Long id) { return new User("bbb", 21, "上海", "aaa@bbb.com"); } @ApiOperation("用户列表") @GetMapping("/users") public List<User> list(@ApiParam("查看第几页") @RequestParam int pageIndex, @ApiParam("每页多少条") @RequestParam int pageSize) { List<User> result = new ArrayList<>(); result.add(new User("aaa", 50, "北京", "aaa@ccc.com")); result.add(new User("bbb", 21, "广州", "aaa@ddd.com")); return result; } @ApiIgnore @DeleteMapping("/users/{id}") public String deleteById(@PathVariable Long id) { return "delete user : " + id; }}@Data@NoArgsConstructor@AllArgsConstructor@ApiModel("用户根本信息")public class User { @ApiModelProperty("姓名") @Size(max = 20) private String name; @ApiModelProperty("年龄") @Max(150) @Min(1) private Integer age; @NotNull private String address; @Pattern(regexp = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$") private String email;}
第五步:启动利用!拜访swagger页面:http://localhost:8080/swagger-ui/index.html
留神:
- 这次更新,移除了原来默认的swagger页面门路:
http://host/context-path/swagger-ui.html
,新增了两个可拜访门路:http://host/context-path/swagger-ui/index.html
和http://host/context-path/swagger-ui/
- 通过调整日志级别,还能够看到新版本的swagger文档接口也有新增,除了以前老版本的文档接口
/v2/api-docs
之外,还多了一个新版本的/v3/api-docs
接口。
本系列教程《Spring Boot 2.x基础教程》点击中转!
代码示例
本文的相干例子能够查看上面仓库中的chapter2-7
目录:
- Github:https://github.com/dyc87112/SpringBoot-Learning/
- Gitee:https://gitee.com/didispace/SpringBoot-Learning/
如果您感觉本文不错,欢送Star
反对,您的关注是我保持的能源!
本文首发:尝鲜刚公布的 SpringFox 3.0.0,以前造的轮子能够不必了...,转载请注明出处。
欢送关注我的公众号:程序猿DD,取得独家整顿的学习资源和日常干货推送。点击中转本系列教程目录。