POM

  1. <!-- Swagger2 Begin -->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. <version>2.9.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>io.springfox</groupId>
  9. <artifactId>springfox-swagger-ui</artifactId>
  10. <version>2.9.0</version>
  11. </dependency>
  12. <!-- Swagger2 End -->

配置文件

  1. package com.limlut.itoken.service.posts.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.builders.PathSelectors;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.ApiInfo;
  8. import springfox.documentation.spi.DocumentationType;
  9. import springfox.documentation.spring.web.plugins.Docket;
  10. @Configuration
  11. public class Swagger2Config {
  12. @Bean
  13. public Docket createRestApi() {
  14. return new Docket(DocumentationType.SWAGGER_2)
  15. .apiInfo(apiInfo())
  16. .select()
  17. .apis(RequestHandlerSelectors.basePackage("com.limlut.itoken.service.posts.controller"))
  18. .paths(PathSelectors.any())
  19. .build();
  20. }
  21. private ApiInfo apiInfo() {
  22. return new ApiInfoBuilder()
  23. .title("iToken API 文档")
  24. .description("iToken API 网关接口,http://www.hzlim.cn")
  25. .termsOfServiceUrl("http://www.hzlim.cn")
  26. .version("1.0.0")
  27. .build();
  28. }
  29. }

启动类添加注解

  1. @EnableSwagger2

使用

  1. @ApiOperation(value = "分页查询")
  2. @ApiImplicitParams({
  3. @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int", paramType = "path"),
  4. @ApiImplicitParam(name = "pageSize", value = "笔数", required = true, dataType = "int", paramType = "path"),
  5. @ApiImplicitParam(name = "tbPostsPostJson", value = "对象 JSON 字符串", required = false, dataTypeClass = String.class, paramType = "json")
  6. })
  • @Api:修饰整个类,描述 Controller 的作用
  • @ApiOperation:描述一个类的一个方法,或者说一个接口
  • @ApiParam:单个参数描述
  • @ApiModel:用对象来接收参数
  • @ApiProperty:用对象接收参数时,描述对象的一个字段
  • @ApiResponse:HTTP 响应其中 1 个描述
  • @ApiResponses:HTTP 响应整体描述
  • @ApiIgnore:使用该注解忽略这个API
  • @ApiError:发生错误返回的信息
  • @ApiImplicitParam:一个请求参数
  • @ApiImplicitParams:多个请求参数