[英]Receiving 404 error on Swagger UI with Spring
我正在将 swagger UI 与 Spring 引导应用程序集成。 当我击中 swagger-ui.html 时。 我收到 404 错误。 我的配置 class 如下:
@Configuration
@EnableSwagger2
//@Import(SwaggerConfiguration.class)
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
...
错误信息:
{"status":404,"message":"HTTP 404 Not Found","link":"https://jersey.java.net/apidocs/2.8/jersey/javax/ws/rs/NotFoundException.html"}
我有一个类似的问题,其中 /swagger-ui.html(或新的端点版本 /swagger-ui/)返回 404,但 /v2/api-docs 返回一个有效的 json。 解决方案是将 swagger 版本从 3.0.0 降级。 到 2.9.2。
希望这会有所帮助,请在下面找到我的工作招摇配置:
@Configuration
@EnableSwagger2
@Profile({"!production"})
public class SwaggerConfiguration extends WebMvcConfigurerAdapter {
@Autowired
private ServletContext servletContext;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.host("localhost")
.directModelSubstitute(LocalDate.class, Date.class)
.pathProvider(new RelativePathProvider(servletContext) {
@Override
public String getApplicationBasePath() {
return "/";
}
})
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
我遇到了同样的问题,通过用 springfox-boot-starter:3.0.0 替换 springfox-swagger2 和 springfox-swagger-ui 依赖项并删除 @EnableSwagger2 注释来修复它。 Please note that the Swagger url also changes from http://localhost:8080/swagger-ui.html to http://localhost:8080/swagger-ui/index.html.
您的配置类之一中有 @EnableWebMvc 吗? 如果是这样,您将不得不手动进行资源配置,如下所示:
@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-ui/
而不是swagger-ui.html
请将以下依赖引入 pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
我在 spring-boot 中使用 springfox-boot-starter:3.0.0 遇到了几乎类似的问题,其中 swagger UI 将变为空并查看它正在抛出 404 的控制台
http://{ip}:{port}/custom/url/swagger-ui/index.html/swagger-ui-bundle.js?v=3.0.0 net::ERR_ABORTED 404
错误是我在文档 URL 末尾添加的“/”
我在写“http://{ip}:{port}/custom/url/swagger-ui/index.html/”
代替
“http://{ip}:{port}/custom/url/swagger-ui/index.html”
注意末尾的“/”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.