1.pom.xml 依赖
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
2.配置属性
#swagger 配置
swagger:
enable: false
base-package: com.rabbit.biz.controller
title: rabbit
description: 例子模板
version: 1.0
CustomSwagger2Properties
/**
* swagger 属性配置
*
* @author yinjianwei
* @date 2018/08/23
*/
@Component
@ConfigurationProperties(prefix = "swagger")
public class CustomSwagger2Properties {
/**
* swagger 是否启用
*/
private Boolean enable;
/**
* 包路径
*/
private String basePackage;
/**
* 标题
*/
private String title;
/**
* 描述
*/
private String description;
/**
* 版本
*/
private String version;
...get set
}
3.自定义配置
CustomSwagger2Configuration
/**
* swagger 配置类
*
* @author yinjianwei
* @date 2018/08/23
*/
@Configuration
@EnableSwagger2
public class CustomSwagger2Configuration {
@Autowired
private CustomSwagger2Properties customSwagger2Properties;
/**
* swagger2的配置文件
*
* @return
*/
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(customSwagger2Properties.getEnable())
.apiInfo(apiInfo())
.select()
// 包路径
.apis(RequestHandlerSelectors.basePackage(customSwagger2Properties.getBasePackage()))
.paths(PathSelectors.any())
.build();
}
/**
* 构建 api文档的详细信息
*
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title(customSwagger2Properties.getTitle())
// 描述
.description(customSwagger2Properties.getDescription())
// 版本号
.version(customSwagger2Properties.getVersion())
.build();
}
}
4.使用
/**
* 试卷
*
* @author yinjianwei
* @date 2017/12/07
*/
@Api(tags = {"试卷"})
@RestController
@RequestMapping(value = "/exam")
public class ExamController {
@Autowired
private ExamService examService;
/**
* 根据条件筛选试卷数据
*
* @param examQuery 查询条件
* @return
*/
@GetMapping("/list_exam_by_condition")
@ApiOperation(value = "根据条件筛选试卷数据", httpMethod = "GET")
@ApiImplicitParams({@ApiImplicitParam(name = "examQuery", value = "查询条件", required = true, dataType = "object")})
public DataResponse listExamByCondition(@RequestBody ExamQuery examQuery) {
List<ExamVO> examVOs = examService.listExamByCondition(examQuery);
return DataResponse.ok(examVOs);
}
}
@Api:用在类上,说明该类的作用。
@ApiOperation:用在方法上,说明方法的作用。
@ApiImplicitParams:描述方法参数。