Swagger

Swagger说明

作业在controller类上
@Api(tags = "3、安全管理")//标题 tags里边不要带有/ 要不然路径
@ApiSort(30) //标题排序
    
作用在具体方法接口上    
@ApiOperation(value = "1、公司安全管理数据总览")//方法说明
@ApiOperationSupport(order=10)//接口排序
    
@ApiOperation(value = "2、安全标准化规范", notes = "type=部门下拉框的id" +"secondType=day" +"secondType=week" +"secondType=month")

notes 接口的详细说明
    
    
//作用在响应类上   
@EqualsAndHashCode(callSuper = true)//调用父类的  b
@ApiModel("隐患排查治理详情")
@Data
public class TaqzltasklsAb extends GdCommonInfo {
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @ApiModelProperty("隐患发现时间")
    private Date yhfxsj;
    @ApiModelProperty("隐患位置")
    private String yhwz;
    @ApiModelProperty("隐患类型")
    private String yhlx;
} 

controller中使用的
请求类中使用的注解

  1. @Api,用在请求的类上,表示对类的说明
    (1)tags=“说明该类的作用,可以在UI界面上看到的注解”
    (2)value=“该参数没什么意义,在UI界面上也看不到,所以不需要配置”

  2. @ApiOperation:用在请求的方法上,说明方法的用途、作用
    (1)value=“说明方法的用途、作用”
    (2) notes=“方法的备注说明”

@ApiImplicitParams:用在请求的方法上,表示一组参数说明

会自动配置 返回pojo类的类型

用于响应类上的注解

  1. @ApiModel:用于响应类上,表示一个返回响应数据的信息
  2. @ApiModelProperty 用在属性上,描述响应类的属性

==========================================================
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述

name 参数名字 可以不写

value 产生作用描述

required 是否必须

@ApiParam(name = "bis_key", value = "bis_key", required = true)

不要单独用apiparam 要不然swagger 调试 不显示参数位置

@ApiModel:用对象来接收参数

@ApiModelProperty:用对象接收参数时,描述对 象的一个字段

@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用 该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数

配置

package com.eagle.common.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.eagle.system"))
                .paths(PathSelectors.any())
                .build()
                .groupName("1、基础配置")
                .pathMapping("/");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("基础配置 API")
                .description("基础配置")
                // 这里可以是项目地址
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }

    @Bean
    public Docket apiBusiness() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfoBusiness())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.eagle.business"))
                .paths(PathSelectors.any())
                .build()
                .groupName("2、业务")
                .pathMapping("/");
    }

    private ApiInfo apiInfoBusiness() {
        return new ApiInfoBuilder().title("业务 API")
                .description("业务")
                // 这里可以是项目地址
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }

    @Bean
    public Docket apiLkyw() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfoLkyw())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.eagle.foreign.lkyw"))
                .paths(PathSelectors.any())
                .build()
                .groupName("3、两客一危")
                .pathMapping("/");
    }

    public ApiInfo apiInfoLkyw() {
        return  new ApiInfoBuilder()
                .title("两客一危 API")
                .description("两客一危")
                // 这里可以是项目地址
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }

    @Bean
    public Docket apiLiuxihe() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfoLiuxihe())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.eagle.foreign.liuxihe"))
                .paths(PathSelectors.any())
                .build()
                .groupName("4、流溪河特大桥")
                .pathMapping("/");
    }

    public ApiInfo apiInfoLiuxihe() {
        return  new ApiInfoBuilder()
                .title("流溪河特大桥 API")
                .description("流溪河特大桥")
                // 这里可以是项目地址
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }

}

访问地址

没有配置就是默认的

/swagger-ui.html#
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}
@EnableSwagger2
有的在启动类 有的在配置类
有的需要资源映射 有的不需要
posted @ 2021-10-29 14:11  李广龙  阅读(183)  评论(0)    收藏  举报