[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
- 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 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
- @ 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 ());
- }
- Private ApiInfo apiInfo (){
- Return new ApiInfoBuilder ()
- . Title ("background interface documentation and testing ")
- . Description ("this is a test document and platform for the app to call the server interface ")
- . 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 Add the head parameter. The foreground effect is as follows:
Original article: http://blog.csdn.net/u014044812/article/details/71473226