Swagger2
maven依赖
<!-- swagger2 配置 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<!-- swagger2 原生UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<!-- swagger2 个性化UI -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.6</version>
</dependency>
Bean配置
package com.imooc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors
.basePackage("com.xxxx.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("xxxxxxxx接口api")
.contact(new Contact("imooc",
"https://www.xxx.com",
"abc@imooc.com"))
.description("前后端交互API文档")
.version("1.0.1")
.termsOfServiceUrl("https://www.xxx.com")
.build();
}
}
使用方式
- @Api():用于Controller类
表示标识这个类是swagger的资源
@Api(value = "地址相关", tags = {"地址相关的api接口"})
@Controller
public class AddressController(){
}
- @ApiOperation():用于Controller的方法上
表示一个http请求的操作
@Api(value = "地址相关", tags = {"地址相关的api接口"})
@Controller
public class AddressController(){
@ApiOperation(value = "根据用户id查询收货地址列表", notes = "根据用户id查询收货地址列表", httpMethod = "POST")
public void list(String userId){
}
}
- @ApiParam():用于方法,参数,字段说明
表示对参数的添加元数据(说明或是否必填等)
@Api(value = "地址相关", tags = {"地址相关的api接口"})
@Controller
public class AddressController(){
@ApiOperation(value = "根据用户id查询收货地址列表", notes = "根据用户id查询收货地址列表", httpMethod = "POST")
public void list(@ApiParam(name = "id", value = "id入参", required = true)String userId){
}
}
- @ApiModel():用于返回对象的类上
表示对类进行说明,用于参数用实体类接收
package com.imooc.pojo.bo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value = "用户对象BO", description = "从客户端,由用户传入的数据封装在此entity中")
public class UserBO {
}
- @ApiModelProperty()用于方法,字段
表示对model属性的说明或者数据操作更改
package com.imooc.pojo.bo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value = "用户对象BO", description = "从客户端,由用户传入的数据封装在此entity中")
public class UserBO {
@ApiModelProperty(value = "用户名", name = "username", example = "imooc", required = true)
private String username;
@ApiModelProperty(value = "密码", name = "password", example = "123456", required = true)
private String password;
@ApiModelProperty(value = "确认密码", name = "confirmPassword", example = "123456", required = false)
private String confirmPassword;
}
- @ApiIgnore():用于类,方法,方法参数
表示这个方法或者类被Swagger忽略
@ApiIgnore
@RestController
public class HelloController {
}
- @ApiImplicitParam():用于方法
表示单独的请求参数
- @ApiImplicitParams():用于方法,包含多个 @ApiImplicitParam
表示请求的参数组
@Api("测试用例1")
@Controller
public class swaggerTestUse(){
@ApiOperation(value = "apiOperationSwaggerTest", notes = "apiOperationSwagger测试")
@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id入参", required = true, dataType = "Integer", paramType = "query"),
@ApiImplicitParam(name = "brand", value = "brand", required = true, dataType = "BRAND", paramType = "body")
})
public void apiOperationSwaggerTest(Integer id, Brand band){
}
}