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
Swagger with Spring Boot 2.0 leads to 404 error page
提问by riorio
I'm trying to integrate my Spring Boot version 2.0.1.RELEASE
with Swagger.
我正在尝试将我的 Spring Boot 版本2.0.1.RELEASE
与Swagger集成 。
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 SwaggerConfig
bean:
并创建了SwaggerConfig
bean:
@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 found
error.
我得到一个page not found
错误。
I found this issues in the swagger github pageand this question in stackoverflowbut I was not able to change my 404
error.
我发现在招摇github上页这问题,并在计算器这个问题,但我无法改变我的404
错误。
采纳答案by riorio
I was able to make it work with Spring boot version 2.0.4.RELEASE
and 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
回答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.html
or
http://localhost:8080/spring-security-rest/swagger-ui.html
试试这个
http://localhost:8080/spring-security-rest/api/swagger-ui.html
或
http://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 文档)
回答by barbarous
Don't forget to change server.contextPath
to server.servlet.contextPath
if you upgrade Spring Boot to 2+.
如果您将 Spring Boot 升级到 2+,请不要忘记更改server.contextPath
为server.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 自动配置。