8.Spring Boot集成Swagger
一、Swagger 是什么?
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。 http://swagger.io/ Springfox 的前身是 swagger-springmvc,是一个开源的 API doc 框架,可以将我们的 Controller 的方法以文档的形式展现,基于 Swagger。 http://springfox.github.io/springfox/
二、添加依赖jar
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>所涉及的文件

三、配置类
util/configuration/Swagger2Configuration.java
http://localhost:8080/swagger-ui.html
五、自定义(注解的使用)
@ApiIgnore
忽略暴露的 api
@ApiOperation(value = "查找", notes = "根据用户 ID 查找用户")
添加说明
其他注解:
@Api:用在类上,说明该类的作用
@ApiImplicitParams:用在方法上包含一组参数说明
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses 中,一般用于表达一个错误的响应信息
code:数字,例如 400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:描述一个 Model 的信息(这种一般用在 post 创建的时候,使用@RequestBody 这样的场景,请求参数无法使用@ApiImplicitParam 注解进行描述的时候)
@ApiModelProperty:描述一个 model 的属性(加在entity实体类的属性上)
更多请查看
https://github.com/swagger-api/swagger-core/wiki/Annotations
附上之前的ApiController参考

Last updated
Was this helpful?