SpringBoot引入Swagger

1, pom文件

  <swagger.version>2.7.0</swagger.version>
  <!--其他版本EnableSwagger2注解找不到,可能是个例,待验证。-->
 -------------------------------------------------              
  <!--swagger start-->
  <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>${swagger.version}</version>
  </dependency>
  <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>${swagger.version}</version>
  </dependency>
  <!--swagger end-->

2,swagger配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("包路径"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("标题")
                .description("描述")
                .termsOfServiceUrl("地址")
                .contact(new Contact("联系人", "", ""))
                .version("1.0")
                .build();
    }

}

3,使用
* 忽略类或方法
@ApiIgnore
* 类注释
@Api(tags = “账单管理”, description = “账单管理API”)
* 方法注释
@ApiOperation(value = “获取最新已出账单”, notes = “获取最新已出账单”)
@ApiImplicitParam(name = “id”, value = “用户账单id”, required = true, dataType = “Long”, paramType = “path”)
paramType类型:path, query, body, header, form
@ApiImplicitParams用于组织多个ApiImplicitParam
如:

@ApiImplicitParams({
@ApiImplicitParam(name = “size”, value = “每页数量”, dataType = “Long”, paramType = “query”),
@ApiImplicitParam(name = “current”, value = “页码”, dataType = “Long”, paramType = “query”)
})

参考:
https://blog.csdn.net/itguangit/article/details/78978296
http://blog.didispace.com/springbootswagger2/

猜你喜欢

转载自blog.csdn.net/u012661496/article/details/82356236