Swagger2 Add the HTTP head parameter, swagger2head

Source: Internet
Author: User

[Switch] Swagger2 adds the HTTP head parameter, swagger2head

Swagger is often used together with JWT, while jwt is usually used to place the token in the head, which makes it inconvenient for us to use swagger for testing, because of cross-origin problems, the header parameter cannot be customized by default. Then I went to the Internet and found that most of the Chinese users wrote a Filter interface and added it to the configuration. This greatly damages the integrity of the program. Think about this as maintaining two sets of code. We only need a simple small function. Most of the foreign countries need to modify the index page of Swagger:

 

[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 is set to false by default. I use swagger2 and do not need to configure static things. Therefore, I added several lines of code in 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 ("token "). 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 ());
  18. }
  19. Private ApiInfo apiInfo (){
  20. Return new ApiInfoBuilder ()
  21. . Title ("background interface documentation and testing ")
  22. . Description ("this is a test document and platform for the app to call the server interface ")
  23. . Version ("1.0.0 ")
  24. . TermsOfServiceUrl ("http://terms-of-services.url ")
  25. //. License ("LICENSE ")
  26. //. LicenseUrl ("http://url-to-license.com ")
  27. . Build ();
  28. }
  29. }


The first four lines of code Add the head parameter. The foreground effect is as follows:



Original article: http://blog.csdn.net/u014044812/article/details/71473226

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.