关于java:尝鲜刚发布的-SpringFox-300以前造的轮子可以不用了

9次阅读

共计 3018 个字符,预计需要花费 8 分钟才能阅读完成。

最近 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
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);
    }

}

第四步:配置一些接口例子,比方:

@Api(tags="用户治理")
@RestController
public 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

留神:

  1. 这次更新,移除了原来默认的 swagger 页面门路:http://host/context-path/swagger-ui.html,新增了两个可拜访门路:http://host/context-path/swagger-ui/index.htmlhttp://host/context-path/swagger-ui/
  2. 通过调整日志级别,还能够看到新版本的 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,取得独家整顿的学习资源和日常干货推送。点击中转本系列教程目录。

正文完
 0