1.pom.xml 依赖

    1. <!-- Swagger -->
    2. <dependency>
    3. <groupId>io.springfox</groupId>
    4. <artifactId>springfox-swagger2</artifactId>
    5. <version>2.8.0</version>
    6. </dependency>
    7. <dependency>
    8. <groupId>io.springfox</groupId>
    9. <artifactId>springfox-swagger-ui</artifactId>
    10. <version>2.8.0</version>
    11. </dependency>

    2.配置属性

    1. #swagger 配置
    2. swagger:
    3. enable: false
    4. base-package: com.rabbit.biz.controller
    5. title: rabbit
    6. description: 例子模板
    7. version: 1.0

    CustomSwagger2Properties

    1. /**
    2. * swagger 属性配置
    3. *
    4. * @author yinjianwei
    5. * @date 2018/08/23
    6. */
    7. @Component
    8. @ConfigurationProperties(prefix = "swagger")
    9. public class CustomSwagger2Properties {
    10. /**
    11. * swagger 是否启用
    12. */
    13. private Boolean enable;
    14. /**
    15. * 包路径
    16. */
    17. private String basePackage;
    18. /**
    19. * 标题
    20. */
    21. private String title;
    22. /**
    23. * 描述
    24. */
    25. private String description;
    26. /**
    27. * 版本
    28. */
    29. private String version;
    30. ...get set
    31. }

    3.自定义配置

    CustomSwagger2Configuration

    1. /**
    2. * swagger 配置类
    3. *
    4. * @author yinjianwei
    5. * @date 2018/08/23
    6. */
    7. @Configuration
    8. @EnableSwagger2
    9. public class CustomSwagger2Configuration {
    10. @Autowired
    11. private CustomSwagger2Properties customSwagger2Properties;
    12. /**
    13. * swagger2的配置文件
    14. *
    15. * @return
    16. */
    17. @Bean
    18. public Docket api() {
    19. return new Docket(DocumentationType.SWAGGER_2)
    20. .enable(customSwagger2Properties.getEnable())
    21. .apiInfo(apiInfo())
    22. .select()
    23. // 包路径
    24. .apis(RequestHandlerSelectors.basePackage(customSwagger2Properties.getBasePackage()))
    25. .paths(PathSelectors.any())
    26. .build();
    27. }
    28. /**
    29. * 构建 api文档的详细信息
    30. *
    31. * @return
    32. */
    33. private ApiInfo apiInfo() {
    34. return new ApiInfoBuilder()
    35. // 页面标题
    36. .title(customSwagger2Properties.getTitle())
    37. // 描述
    38. .description(customSwagger2Properties.getDescription())
    39. // 版本号
    40. .version(customSwagger2Properties.getVersion())
    41. .build();
    42. }
    43. }

    4.使用

    1. /**
    2. * 试卷
    3. *
    4. * @author yinjianwei
    5. * @date 2017/12/07
    6. */
    7. @Api(tags = {"试卷"})
    8. @RestController
    9. @RequestMapping(value = "/exam")
    10. public class ExamController {
    11. @Autowired
    12. private ExamService examService;
    13. /**
    14. * 根据条件筛选试卷数据
    15. *
    16. * @param examQuery 查询条件
    17. * @return
    18. */
    19. @GetMapping("/list_exam_by_condition")
    20. @ApiOperation(value = "根据条件筛选试卷数据", httpMethod = "GET")
    21. @ApiImplicitParams({@ApiImplicitParam(name = "examQuery", value = "查询条件", required = true, dataType = "object")})
    22. public DataResponse listExamByCondition(@RequestBody ExamQuery examQuery) {
    23. List<ExamVO> examVOs = examService.listExamByCondition(examQuery);
    24. return DataResponse.ok(examVOs);
    25. }
    26. }

    @Api:用在类上,说明该类的作用。
    @ApiOperation:用在方法上,说明方法的作用。
    @ApiImplicitParams:描述方法参数。