1) 引入Swagger2和swagger ui (在父工程中依赖)
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> |
2) 编写配置类(@EnableSwagger2 开启swagger2自动生成api文档功能)
package com.atguigu.user.config;
import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2 //开启swagger2自动生成api文档的功能 @Configuration public class AppSwaggerConfig {
} |
http://localhost:8080/swagger-ui.html
n @Api(tags="")
u 用在请求的类上,表示对类的说明
u tags"说明该类的作用,可以在UI界面上看到的注解"
n @ApiOperation(value="")
u 用在请求的方法上,说明方法的用途、作用
u value="说明方法的用途、作用"
n @ApiImplicitParams
u 用在请求的方法上,表示一组参数说明
u @ApiImplicitParam
l @ApiImplicitParam:指定一个请求参数的各个方面
l value:参数的汉字说明、解释
l required:参数是否必须传
l paramType:参数放在哪个地方
n header –> 请求头的获取:@RequestHeader
n query –> 请求参数的获取:@RequestParam
n path(用于restful接口)–> 请求路径变量的获取:@PathVariable
n body(不常用)
n form(不常用)
l dataType:参数类型,默认String,其它值dataType="Integer"
l defaultValue:参数的默认值
n @ApiResponses
u 用在请求的方法上,表示一组响应
u @ApiResponse
l 用在@ApiResponses中,一般用于表达一个错误的响应信息
l code:数字,例如400
l message:信息,例如"请求参数没填好"
l response:抛出异常的类
n @ApiModel
u 主要有两种用途:
u 用于响应类上,表示一个返回响应数据的信息
u 入参实体:使用@RequestBody这样的场景, 请求参数无法使用@ApiImplicitParam注解进行描述的时候
u @ApiModelProperty
l 用在属性上,描述响应类的属性
原文:https://www.cnblogs.com/wangyang9629/p/12861060.html