一、什麼是swagger-resources404錯誤
在使用Swagger時,有時候會遇到swagger-resources404錯誤。這個錯誤的出現通常是因為Swagger的資源文件沒有正確加載,或者是資源文件的路徑不正確。
二、解決swagger-resources404錯誤的方法
1、檢查Swagger的版本號
Swagger的不同版本可能會導致資源文件加載失敗,因此最好檢查所使用的Swagger版本是否與項目兼容。
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency>
2、檢查Swagger資源文件的路徑
檢查Swagger資源文件的路徑是否正確,有時候項目的配置文件可能會導致路徑出錯。
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui.html**") .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
3、檢查Swagger配置文件是否正確
檢查Swagger配置文件(swagger.properties)是否正確,有時候配置文件中的一些參數可能會導致資源文件加載失敗。
swagger.enabled=true swagger.title=API Documentation swagger.description=API Documentation swagger.version=1.0.0 swagger.contact.name=API Support swagger.contact.email=support@api.com swagger.license.name=Apache 2.0 swagger.license.url=http://www.apache.org/licenses/LICENSE-2.0.html swagger.base-path=/** swagger.host=localhost:8080 swagger.path-mapping=/api/** swagger.use-default-response-messages=false
4、檢查Spring Boot配置文件是否正確
檢查Spring Boot配置文件(application.properties)是否正確,有時候配置文件中的一些參數可能會導致資源文件加載失敗。
spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp # swagger-ui配置 springfox.documentation.swagger-ui.groupNames=abc springfox.documentation.swagger-ui.enabled=true
三、總結
Swagger-resources404錯誤的解決方法可以從多個方面入手,包括檢查Swagger版本號、Swagger資源文件路徑、Swagger配置文件和Spring Boot配置文件。只要找到錯誤的根源並及時解決,就可以避免這個錯誤的發生。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/286480.html