Solve the problem of swagger reporting error 404
When writing swagger, the startup class did not report an error. When accessing on port 8080, an error 404 was not found.
The original dependencies and classes are as follows
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
@Configuration
@EnableSwagger2
public class SwaggerConfig{
@Bean
public Docket getDocket(){
// docket封装接口文档信息
//封面信息
ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
apiInfoBuilder.title("锋迷商城后端接口说明文档")
.description("此文档详细说明了锋迷商城后端接口规范")
.version("v 2.0.1")
.contact(new Contact("zzd","www.liangge.com","[email protected]"));
ApiInfo apiInfo = apiInfoBuilder.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.select()
.apis(RequestHandlerSelectors.basePackage("com.qfedu.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
After searching online, I learned that MVC did not find swagger-ui.html in the swagger-ui package
Then I wrote the WebConfig class
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
// 解决 SWAGGER 404报错
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
But it still reports an error, which is really a big head.
Later, I checked the information and read the article, and concluded that there may be some mismatch between the swagger version and the springboot version. After that, I changed the version of swagger and swagger-ui in the pom from the original 3.0.0 to 2.9.2. The magic is that it works!