Java Swagger with Spring Boot 2.0 导致 404 错误页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50618350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:43:23  来源:igfitidea点击:

Swagger with Spring Boot 2.0 leads to 404 error page

javaspring-bootswagger

提问by riorio

I'm trying to integrate my Spring Boot version 2.0.1.RELEASEwith Swagger.

我正在尝试将我的 Spring Boot 版本2.0.1.RELEASESwagger集成 。

From this blog postit seemed like it will be easy by just adding two Maven dependencies and everything should work.

从这篇博客文章看来,只需添加两个 Maven 依赖项就很容易了,一切都应该可以正常工作。

So I added the following dependencies to the pom:

所以我在 pom 中添加了以下依赖项:

        <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.8.0</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.8.0</version>
    </dependency>

And created the SwaggerConfigbean:

并创建了SwaggerConfigbean:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
    Docket docket = new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build();

    return docket;
   }
}

And in the properties file I ended up with these 3 entries during the attempts to make it work:

在属性文件中,我在尝试使其工作时得到了这 3 个条目:

spring.application.name=cat-service
management.server.servlet.context-path=/cat-service
server.servlet.contextPath=/cat-service

But at the end, when accessing

但最后,当访问

http://localhost:8080/cat-service/api/v2/api-docs

http://localhost:8080/cat-service/api/v2/api-docs

or the UI page at

或 UI 页面位于

http://localhost:8080/cat-service/swagger-ui.html

http://localhost:8080/cat-service/swagger-ui.html

I get a page not founderror.

我得到一个page not found错误。

I found this issues in the swagger github pageand this question in stackoverflowbut I was not able to change my 404error.

我发现在招摇github上页这问题,并在计算器这个问题,但我无法改变我的404错误。

采纳答案by riorio

I was able to make it work with Spring boot version 2.0.4.RELEASEand this blog post:

我能够使其与 Spring boot 版本2.0.4.RELEASE这篇博文一起使用

I added these dependencies:

我添加了这些依赖项:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

And this configuration file:

而这个配置文件:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SpringFoxConfig {
    @Bean
    public Docket apiDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

And it worked.

它奏效了。

The Swagger UI can be reached at /swagger-ui.html#

Swagger UI 可以在 /swagger-ui.html# 访问

回答by Illary Huaylupo

This worked for me, I used the WebMvcConfigurer instead of the WebMvcConfigurerAdapter because that class is already deprecated.

这对我有用,我使用 WebMvcConfigurer 而不是 WebMvcConfigurerAdapter 因为该类已被弃用。

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
  @Bean
  public Docket productApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()                
            .apis(RequestHandlerSelectors.basePackage("com.illary.controller"))
            .paths(PathSelectors.any())
            .build()            
            .apiInfo(metaData());
  }
  private ApiInfo metaData() {
    return new ApiInfoBuilder()
            .title("Spring Boot Swagger App")
            .description("\"Spring Boot Swagger Server App\"")
            .version("1.0.0")
            .license("Apache License Version 2.0")
            .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
            .build();
  }

  public ApiInfo apiInfo() {
    final ApiInfoBuilder builder = new ApiInfoBuilder();
    builder.title("Swagger Test App").version("1.0").license("(C) Copyright Test")
    .description("The API provides a platform to query build test swagger api");

    return builder.build();
  }

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

回答by gstackoverflow

It became working for me after removing @EnableWebMvc

删除后它对我有用 @EnableWebMvc

回答by Camilo

Another possibility is the location of your Swagger config file; you need to place it at the same package or a subpackage of the spring boot file's. Like the above picture:

另一种可能性是您的 Swagger 配置文件的位置;你需要把它放在同一个包或spring boot文件的子包中。如上图:

Swagger config and Spring Boot file hierarchy

Swagger 配置和 Spring Boot 文件层次结构

回答by Exel Staderlin

First add SwaggerConfig.java file at the same package of your springboot file like the following example.

首先在 springboot 文件的同一个包中添加 SwaggerConfig.java 文件,如下例所示。

@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig extends WebMvcConfigurerAdapter {                                    
    @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/");

       registry.addResourceHandler("/webjars/**")
       .addResourceLocations("classpath:/META-INF/resources/webjars/");
   }

}

try this http://localhost:8080/spring-security-rest/api/swagger-ui.htmlor http://localhost:8080/spring-security-rest/swagger-ui.html

试试这个 http://localhost:8080/spring-security-rest/api/swagger-ui.htmlhttp://localhost:8080/spring-security-rest/swagger-ui.html

If that does not work, try to change the path in application.properties

如果这不起作用,请尝试更改 application.properties 中的路径

Add this to application.properties:

将此添加到 application.properties:

server.servlet-path=/loop-service

and try the following urls:

并尝试以下网址:

http://localhost:8080/loop-service/swagger-ui.html(UI Docs)

http://localhost:8080/loop-service/swagger-ui.html(用户界面文档)

http://localhost:8080/loop-service/v2/api-docs(JSON Docs)

http://localhost:8080/loop-service/v2/api-docs(JSON 文档)

Result : enter image description here

结果 : 在此处输入图片说明

回答by barbarous

Don't forget to change server.contextPathto server.servlet.contextPathif you upgrade Spring Boot to 2+.

如果您将 Spring Boot 升级到 2+,请不要忘记更改server.contextPathserver.servlet.contextPath

回答by O.Dmitry

Solution:You just need to remove @EnableWebMvcfrom the configuration classes.

解决方案:您只需要从配置类中删除@EnableWebMvc

Description:@EnableWebMvcturn on the class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport. In spring-boot, there is an autoconfiguration class org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration, which has the annotation @ConditionalOnMissingBean(WebMvcConfigurationSupport.class).

说明:@EnableWebMvc打开类org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport。在 spring-boot 中,有一个自动配置类org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,它有注解@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

What we get in the end: By adding @EnableWebMvcto the project, we ourselves become responsible for everything, since we turn off spring-boot auto-configuration.

我们最终得到的是:通过将@EnableWebMvc添加到项目中,我们自己负责一切,因为我们关闭了 spring-boot 自动配置。