swagger加请求头带token


原文链接:https://blog.csdn.net/ssjq123/article/details/82592022

现在大家一般都是使用SpringBoot写RESTful接口, 但是在测试带有token的请求的时候, 就有点难受了. 传统的PostMan就有点让人炸毛了.  但是现在Swagger出现了.(SpringBoot简直和Swagger是天作之和)

swagger的整合细节就不在这里说了, 下面进入正题:

  1.  
    @Configuration
  2.  
    public class Swagger2Config {
  3.  
    @Bean
  4.  
    public Docket createRestApi() {
  5.  
    //添加head参数配置start
  6.  
    ParameterBuilder tokenPar = new ParameterBuilder();
  7.  
    List <Parameter> pars = new ArrayList<>();
  8.  
    tokenPar.name("Authorization").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
  9.  
    pars.add(tokenPar.build());
  10.  
    //添加head参数配置end
  11.  
    return new Docket(DocumentationType.SWAGGER_2)
  12.  
    .apiInfo(apiInfo())
  13.  
    .select()
  14.  
    .apis(RequestHandlerSelectors.basePackage("com.cinsc.MainView.ctr"))
  15.  
    .paths(PathSelectors.any())
  16.  
    .build()
  17.  
    .globalOperationParameters(pars);//注意这里
  18.  
    }
  19.  
     
  20.  
    private ApiInfo apiInfo() {
  21.  
    return new ApiInfoBuilder()
  22.  
    .title("springboot利用swagger构建api文档")
  23.  
    .description("束手就擒--简单优雅的restfun风格")
  24.  
    .termsOfServiceUrl("https://blog.csdn.net/ssjq123")
  25.  
    .version("1.0")
  26.  
    .build();
  27.  
    }
  28.  
    }

效果图:

 

多出来一个 Authorization,在里面放token就行了.


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM