swagger 中加入 令牌

1、普通的 swagger 配置

@Configuration
@EnableSwagger2
@ComponentScan("cn.com.blackview.iov.business.web.api")
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        //添加head参数end
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .paths(Predicates.not(PathSelectors.regex("/error.*")))// 错误路径不监控
                .apis(RequestHandlerSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("中间服务")
                .description("提供图片查询,发送短信")
                //版本号
                .version("1.0")
                //描述
                .build();
    }
}

2、消息头 加入 token

@Configuration
@EnableSwagger2
@ComponentScan("cn.com.blackview.iov.business.web.api")
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        //添加head参数start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        //添加head参数end
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .paths(Predicates.not(PathSelectors.regex("/error.*")))// 错误路径不监控
                .apis(RequestHandlerSelectors.any())
                .build()
                .globalOperationParameters(pars)
                .apiInfo(apiInfo());
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("中间服务")
                .description("提供图片查询,发送短信")
                //版本号
                .version("1.0")
                //描述
                .build();
    }
}
发布了20 篇原创文章 · 获赞 0 · 访问量 9178

猜你喜欢

转载自blog.csdn.net/qq_30346433/article/details/102567925