Spring Boot 集成 Swagger 文档
约 182 个字 170 行代码 3 张图片 预计阅读时间 3 分钟
引入依赖项
安装依赖后即可使用:
- 网页:
http://server:port/context-path/swagger-ui.html - JSON:
http://server:port/context-path/v3/api-docs
应用
在控制器上应用
在实体类上应用
Request Body 部分的 Example Value 如下:
可以看到,遵循了设定的默认值和给定值。
切换到 Schema,可以看到详细的信息,包括限制:
不同的注解
一些文档里面使用的是 SpringFox,其中的注解和 springdoc 不一样,使用时要注意(左 SpringFox,右 springdoc):
@Api→@Tag@ApiIgnore→@Parameter(hidden = true)or@Operation(hidden = true)or@Hidden@ApiImplicitParam→@Parameter@ApiImplicitParams→@Parameters@ApiModel→@Schema@ApiModelProperty(allowEmptyValue = true)→@Schema(nullable = true)@ApiModelProperty→@Schema@ApiOperation(value = "foo", notes = "bar")→@Operation(summary = "foo", description = "bar")@ApiParam→@Parameter@ApiResponse(code = 404, message = "foo")→@ApiResponse(responseCode = "404", description = "foo")


