简体   繁体   English

在 Spring 的 Swagger UI 上接收 404 错误

[英]Receiving 404 error on Swagger UI with Spring

I am integrating with swagger UI with Spring boot application.我正在将 swagger UI 与 Spring 引导应用程序集成。 When I hit the swagger-ui.html.当我击中 swagger-ui.html 时。 I am getting the 404 error.我收到 404 错误。 My config class is below:我的配置 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();
        }
...

Error message:错误信息:

{"status":404,"message":"HTTP 404 Not Found","link":"https://jersey.java.net/apidocs/2.8/jersey/javax/ws/rs/NotFoundException.html"}

I had a similar problem where /swagger-ui.html (or the new endpoint version /swagger-ui/) returned 404, but the /v2/api-docs returned a valid json.我有一个类似的问题,其中 /swagger-ui.html(或新的端点版本 /swagger-ui/)返回 404,但 /v2/api-docs 返回一个有效的 json。 The solution was to downgrade the swagger version from 3.0.0.解决方案是将 swagger 版本从 3.0.0 降级。 to 2.9.2.到 2.9.2。

Hope this helps, please find my working swagger config below:希望这会有所帮助,请在下面找到我的工作招摇配置:

@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();
    }
}

I had the same problem and it was fixed by replacing the springfox-swagger2 and springfox-swagger-ui dependencies with springfox-boot-starter:3.0.0 and removing the @EnableSwagger2 annotation.我遇到了同样的问题,通过用 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. Please note that the Swagger url also changes from http://localhost:8080/swagger-ui.html to http://localhost:8080/swagger-ui/index.html.

Do you have @EnableWebMvc in one of your configuration classes?您的配置类之一中有 @EnableWebMvc 吗? If so, you will have to do the resources configuration manually, like below:如果是这样,您将不得不手动进行资源配置,如下所示:

@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

Please invite following dependence into pom.xml请将以下依赖引入 pom.xml

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

I had an almost similar issue in spring-boot using the springfox-boot-starter:3.0.0 where swagger UI would come as empty and looking at the console it was throwing 404我在 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 http://{ip}:{port}/custom/url/swagger-ui/index.html/swagger-ui-bundle.js?v=3.0.0 net::ERR_ABORTED 404

The error was the "/" that I was adding at the end of the documentation URL错误是我在文档 URL 末尾添加的“/”

I was writing "http://{ip}:{port}/custom/url/swagger-ui/index.html/"我在写“http://{ip}:{port}/custom/url/swagger-ui/index.html/”

Instead of代替

"http://{ip}:{port}/custom/url/swagger-ui/index.html" “http://{ip}:{port}/custom/url/swagger-ui/index.html”

Note the "/" at the end注意末尾的“/”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM