spring boot 配置swagger UI

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());  
     }

}

 

posted @ 2018-01-24 18:12  pkyou  阅读(376)  评论(0编辑  收藏  举报