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
- Window.swaggerui = New Swaggerui ({
- Discoveryurl: "Http://pathtomyservice.com/resources",
- Headers: {"Testheader": "123"},
- ApiKey: "123",
- Apikeyname: "Api-key",
- dom_id: "Swagger-ui-container",
- Supportheaderparams:true,
- Supportedsubmitmethods: [' Get ', ' post ', ' Put ', ' delete '],
- Oncomplete:function (Swaggerapi, Swaggerui) {
- if (console) {
- Console.log ("Loaded Swaggerui");
- Console.log (SWAGGERAPI);
- Console.log (Swaggerui);
- }
- $ (' Pre code '). each (function (I, E) {Hljs.highlightblock (e)});
- },
- Onfailure:function (data) {
- if (console) {
- Console.log ("Unable to Load Swaggerui");
- Console.log (data);
- }
- },
- Docexpansion: "None"
- });
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
- @EnableSwagger2
- @EnableWebMvc
- @ComponentScan ("Com.g.web")
- Public class Swaggerconfig {
- @Bean
- Public Docket API () {
- ParameterBuilder Tokenpar = new ParameterBuilder ();
- List<parameter> pars = new arraylist<parameter> ();
- Tokenpar.name ("x-access-token"). Description ("token"). Modelref (new Modelref ("string")). ParameterType ("header"). Required (false). Build ();
- Pars.add (Tokenpar.build ());
- return new Docket (documentationtype.swagger_2)
- . Select ()
- . APIs (Requesthandlerselectors.any ())
- . Paths (Pathselectors.regex ("/api/.*"))
- . Build ()
- . Globaloperationparameters (Pars)
- . Apiinfo (Apiinfo ());
- }
- private Apiinfo Apiinfo () {
- return New Apiinfobuilder ()
- . Title ("Background interface documentation and testing")
- . Description ("This is a test document and platform that calls the server-side interface to the app-side")
- . Version ("1.0.0")
- . Termsofserviceurl ("Http://terms-of-services.url")
- //.license ("license")
- //.licenseurl ("http://url-to-license.com")
- . build ();
- }
- }
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