spring boot 配置swagger UI

送分小仙女□ 提交于 2021-02-01 11:35:44

springboot集成swaggerUI

有这样的需求

1.在每个接口上都增加一个字段;

2.接口文档只展示满足一定条件URL的接口

配置文件

详细看代码

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.service.Parameter;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    ApiInfo apiInfo(){
        return new ApiInfoBuilder().title("显示的标题").description("标题描述").build();
    }
    
    @Bean  
    public Docket api(){  
        ParameterBuilder tokenPar = new ParameterBuilder();  
        List<Parameter> pars = new ArrayList<Parameter>();  
        
        tokenPar.name("Authorization").description("token").modelRef(new ModelRef("string")).defaultValue("").parameterType("header").required(false).build();  
        pars.add(tokenPar.build());  
        return new Docket(DocumentationType.SWAGGER_2)  
             .select()  
             .apis(RequestHandlerSelectors.any())  
             .paths(PathSelectors.regex("/go(d|to)/.*"))
             .build()  
             .globalOperationParameters(pars)  
             .apiInfo(apiInfo());  
     }

}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!