Today I facing a problem that all my services(more than 20 spring boot microservice) swagger UI could not access. The swagger UI works fine since last 2 years. When I am using this command to access swagger UI:
root@soa-report-analysis-zhuolian-7f5ff8fd66-lwbjh:/# curl http://127.0.0.1:11012/swagger-ui.html
{"timestamp":"2021-09-15T08:30:42.531+0000","status":404,"error":"Not Found","message":"No message available","path":"/swagger-ui.html"}
I also tried:
http://127.0.0.1:11012/swagger-ui.html#
http://127.0.0.1:11012/swagger-ui/
both did not work. My swagger version info look like this:
api "io.springfox:springfox-swagger2:3.0.0"
api "io.springfox:springfox-swagger-ui:3.0.0"
And config resource like this:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
SwaggerConfig:
package com.sportswin.soa.misc.config.swagger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* swagger
*
* @author yangxinhua
* @version 1.0
* @date 2019-04-25 14:52
*/
@Slf4j
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Value("${swagger.show:false}")
private Boolean swaggerShow;
@Bean
public Docket createRestApi() {
ParameterBuilder tokenPar = new ParameterBuilder();
ParameterBuilder tokenPar2 = new ParameterBuilder();
ParameterBuilder tokenPar3 = new ParameterBuilder();
ParameterBuilder tokenPar4 = new ParameterBuilder();
ParameterBuilder tokenPar5 = new ParameterBuilder();
ParameterBuilder tokenPar6 = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
tokenPar.name("token").defaultValue("70e624c1172ee53801ee8695c72030d5").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
tokenPar2.name("appCode").defaultValue("101").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
tokenPar3.name("requestId").defaultValue("2e0dddf9-17d9-42d9-8614-b44de17fe135").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
tokenPar4.name("clientId").defaultValue("7c54ff40-f11e-41c0-8d31-4b26d6569b5a").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
tokenPar5.name("x-request-info")
.defaultValue("appCode=202;token=70e624c1172ee53801ee8695c72030d;clientId=7c54ff40-f11e-41c0-8d31-4b26d6569b5a;requestId=2e0dddf9-17d9-42d9-8614-b44de17fe135")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build();
tokenPar6.name("appVersion")
.defaultValue("Unknown")
.modelRef(new ModelRef("String"))
.parameterType("header")
.required(false)
.build();
tokenPar6.name("platform")
.defaultValue("WB")
.modelRef(new ModelRef("String"))
.parameterType("header")
.required(false)
.build();
pars.add(tokenPar.build());
pars.add(tokenPar2.build());
pars.add(tokenPar3.build());
pars.add(tokenPar4.build());
pars.add(tokenPar5.build());
pars.add(tokenPar6.build());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(swaggerShow)
.select()
.paths(PathSelectors.any())
.build()
.globalOperationParameters(pars);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("service doc")
.termsOfServiceUrl("http://envelope.example.com/swagger-ui.html#/")
.contact(new Contact("dabai", "http://envelope.example.com/swagger-ui.html#/", ""))
.version("1.0")
.build();
}
}
What make me confusing is that:
- why suddenly did not work with swagger UI(return 404 when access the swagger-ui.html)?
- why suddenly did not work with all my service swagger UI, even some servcie did not change(restart or update)? the swagger have a cache and all services cache expired at the same time?
by the way, the config is work and the default show switch is open. I already debug to make sure the initial workflow works as expect. all the config has been initialed.