Swagger2 添加HTTP head參數,swagger2head

來源:互聯網
上載者:User

【轉】Swagger2 添加HTTP head參數,swagger2head

大家使用swagger往往會和JWT一起使用,而一般使用jwt會將token放在head裡,這樣我們在使用swagger測試的時候並不方便,因為跨域問題它預設不能自訂head參數。然後自己去網上找,發現國內大多數的都是寫一個Filter介面,然後添加到配置。這樣極大的破壞了程式的完整性。想想這相當於維護兩套代碼。我們只是需要一個簡單的小功能,國外大多是修改Swagger的index頁面:

 

[html] view plain copy
  1. window.swaggerUi = new SwaggerUi({  
  2.             discoveryUrl: "http://pathtomyservice.com/resources",  
  3.                 headers: { "testheader" : "123" },  
  4.                 apiKey: "123",  
  5.                 apiKeyName: "Api-Key",  
  6.                 dom_id:"swagger-ui-container",  
  7.                 supportHeaderParams: true,  
  8.                 supportedSubmitMethods: ['get', 'post', 'put', 'delete'],  
  9.                 onComplete: function(swaggerApi, swaggerUi){  
  10.                     if(console) {  
  11.                         console.log("Loaded SwaggerUI");  
  12.                         console.log(swaggerApi);  
  13.                         console.log(swaggerUi);  
  14.                     }  
  15.                   $('pre code').each(function(i, e) {hljs.highlightBlock(e)});  
  16.                 },  
  17.                 onFailure: function(data) {  
  18.                     if(console) {  
  19.                         console.log("Unable to Load SwaggerUI");  
  20.                         console.log(data);  
  21.                     }  
  22.                 },  
  23.                 docExpansion: "none"  
  24.             });  

supportHeaderParams預設為false,而我用的是swagger2,不需要配置靜態那些東西,所以我在SwaggerConfig添加了幾行代碼:

 

[java] view plain copy
  1. @EnableSwagger2  
  2. @EnableWebMvc  
  3. @ComponentScan("com.g.web")  
  4. public class SwaggerConfig {  
  5.     @Bean  
  6.     public Docket api(){  
  7.         ParameterBuilder tokenPar = new ParameterBuilder();  
  8.         List<Parameter> pars = new ArrayList<Parameter>();  
  9.         tokenPar.name("x-access-token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();  
  10.         pars.add(tokenPar.build());  
  11.         return new Docket(DocumentationType.SWAGGER_2)  
  12.             .select()  
  13.             .apis(RequestHandlerSelectors.any())  
  14.             .paths(PathSelectors.regex("/api/.*"))  
  15.             .build()  
  16.             .globalOperationParameters(pars)  
  17.             .apiInfo(apiInfo());  
  18.     }  
  19.   
  20.     private ApiInfo apiInfo() {  
  21.         return new ApiInfoBuilder()  
  22.             .title("後台介面文檔與測試")  
  23.             .description("這是一個給app端人員調用server端介面的測試文檔與平台")  
  24.             .version("1.0.0")  
  25.             .termsOfServiceUrl("http://terms-of-services.url")  
  26.             //.license("LICENSE")  
  27.             //.licenseUrl("http://url-to-license.com")  
  28.             .build();  
  29.     }  
  30. }  


前四行代碼是添加head參數的,前台效果是這樣的:



原文:http://blog.csdn.net/u014044812/article/details/71473226

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.