"Go" Swagger2 add http head parameter

Source: Internet
Author: User

The use of swagger is often used in conjunction with JWT, while the general use of JWT will place tokens in the head, which is not convenient when using the swagger test, because cross-domain problems it does not customize the head parameter by default. And then go online to find, found that most of the domestic is to write a filter interface, and then add to the configuration. This greatly destroys the integrity of the program. Consider this equivalent to maintaining two sets of code. We just need a simple little feature, most of the foreign is to modify the index page of swagger:

[HTML]View PlainCopy
  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 defaults to False, and I'm using swagger2, I don't need to configure static stuff, so I added a few lines of code to the Swaggerconfig:

[Java]View PlainCopy
  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 (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 that calls the server-side interface to the app-side")
  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 are added head parameters, the foreground effect is this:



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

"Go" Swagger2 add http head parameter

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.