When we use swagger, we often use it together with JWT, but JWT will put the token in the head, which is not convenient when we use swagger for testing, because it cannot customize the head parameter by default for cross domain problems. Then I went to the Internet and found that most of the domestic users write a Filter interface and add it to the configuration. This greatly destroys the integrity of the program. Think of it as maintaining two sets of code. We just need a simple small function. Most of the foreign countries modify the index page of swagger:
- 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"
- });
The default value of supportHeaderParams is false, and I use swagger2. I don't need to configure static things, so I added a few lines of code in SwaggerConfig:
- @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 gift app End personnel call server Test document and platform of end 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: http://blog.csdn.net/u014044812/article/details/71473226