由于springfox swagger
在最新的springboot 2.6.x
版本中频频报错无法使用,因此计划迁移至springdoc
。
这里仅记录个人使用经验,更具体的信息见官方文档。
一、导入依赖
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.13</version>
</dependency>
仅需导入这一个依赖即可
二、运行
-
springdoc
无需注解开启,正常启动项目即可 -
默认访问路径与
swagger2
一致:http://server:port/context-path/swagger-ui.html
-
也可在
application.yml
中自定义访问路径,例:springdoc: swagger-ui: path: /swagger-ui
三、注解
-
swagger2
所对应注解,官方文档中有详细介绍,这里只写几个常用的 -
@ApiModel(value = "")
->@Schema(title = "")
@ApiModelProperty(value = "")
->@Schema(title = "")
springfox: @ApiModel(value= "User") public class User { @ApiModelProperty(value = "姓名") private String name; } springdoc: @Schema(title = "User") public class User { @Schema(title = "姓名") private String name; }
-
@Api(tags = "")
->@Tag(name = "")
@ApiOperation(value = "", tags = {""})
->@Operation(summary = "", tags = "")
springfox: @Api(tags = "TestController API") @RestController public class TestController { @ApiOperation(value = "根据ID查询", tags = "TestController API") @GetMapping(value = "/{id}") public Result selectOne(@PathVariable Long id) { ... } } springdoc: @Tag(name = "TestController API") @RestController public class TestController { @Operation(summary = "根据ID查询", tags = "TestController API") @GetMapping(value = "/{id}") public Result selectOne(@PathVariable Long id) { ... } }