Java Spring Boot 2.0的招摇过市导致404错误页面
我正在尝试将我的Spring Boot版本Java Spring Boot 2.0的招摇过市导致404错误页面,java,spring-boot,swagger,Java,Spring Boot,Swagger,我正在尝试将我的Spring Boot版本2.0.1.RELEASE与集成 由此看来,只需添加两个Maven依赖项就很容易了,一切都应该可以正常工作 因此,我在pom中添加了以下依赖项: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <v
2.0.1.RELEASE
与集成
由此看来,只需添加两个Maven依赖项就很容易了,一切都应该可以正常工作
因此,我在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>
在属性文件中,在尝试使其工作时,我得到了以下3个条目:
spring.application.name=cat-service
management.server.servlet.context-path=/cat-service
server.servlet.contextPath=/cat-service
但在最后,当访问
http://localhost:8080/cat-服务/api/v2/api文档
或位于的UI页面
http://localhost:8080/cat-service/swagger ui.html
我发现一个页面未找到
错误
我找到了,但无法更改我的
404
错误。这对我来说很有效,我使用了WebMvcConfigurer而不是WebMVCConfigureAdapter,因为该类已被弃用
@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/");
}
}
我能够使用Spring boot版本
2.0.4。发布版
和:
我添加了以下依赖项:
<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>
它成功了
可以通过/Swagger UI.html#访问Swagger UI,它在删除
@EnableWebMvc
后开始为我工作。另一种可能是您的Swagger配置文件的位置;您需要将它放在spring启动文件的同一个包或子包中。
如上图所示:
首先在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/");
}
}
试试这个
http://localhost:8080/spring-security rest/api/swagger ui.html
或
http://localhost:8080/spring-security rest/swagger ui.html
如果不起作用,请尝试在application.properties中更改路径
将此添加到application.properties:
server.servlet-path=/loop-service
并尝试以下URL:
http://localhost:8080/loop-service/swagger ui.html
(ui文档)
http://localhost:8080/loop-服务/v2/api文档
(JSON文档)
结果:
如果将Spring Boot升级到2+,请不要忘记将
server.contextPath
更改为server.servlet.contextPath
解决方案:您只需要从配置类中删除@EnableWebMvc
说明:@EnableWebMvc打开类org.springframework.web.servlet.config.annotation.webmvc配置支持。在SpringBoot中,有一个自动配置类org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,它具有注释@conditionalnmissingbean(WebMvcConfigurationSupport.class)
我们最终得到的结果是:通过在项目中添加@EnableWebMvc,我们自己负责所有事情,因为我们关闭了spring boot自动配置。面临着刚刚解决的依赖关系变化问题 提及 编译“io.springfox:springfox-swaggger2:2.9.2” 以前正在使用-不工作
编译组:'io.springfox',名称:'springfox-swagger2',版本:'3.0.0'在
应用程序.properties中添加以下行后,它开始工作
spring.web.resources.static-locations: classpath:/webapp/
但我不知道我们为什么要加上这个。
添加代码,我认为这可能是相关的。依赖项如下所示:
org.springframework.boot
spring启动程序父级
2.4.1
...
org.springframework.boot
弹簧靴起动器
org.springframework.boot
SpringBootStarterWeb
javax.validation
验证api
2.0.1.最终版本
伊奥·斯普林福克斯
springfox-Swagger 2
2.9.2
伊奥·斯普林福克斯
springfox招摇过市用户界面
2.9.2
org.springframework.boot
弹簧起动试验
测试
主类为
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.context.annotation.Bean;
导入springfox.documentation.builders.PathSelector;
导入springfox.documentation.builders.RequestHandlerSelectors;
导入springfox.documentation.spi.DocumentationType;
导入springfox.documentation.spring.web.plugins.Docket;
导入springfox.documentation.swagger 2.annotations.enableSawagger 2;
@SpringBoot应用程序
@使能招摇过市2
公共类代理应用程序{
公共静态void main(字符串[]args){
run(ProxyApplication.class,args);
}
@豆子
公开摘要api(){
返回新摘要(DocumentationType.SWAGGER_2)
.选择()
.api(RequestHandlerSelectors.basePackage(“com.ghsatpute.proxy”))
.path(路径选择器.any())
.build();
}
}
如果可以帮助某人,则添加此项。这个网址对我有用
http://localhost:8003/v2/api-docs
对于大摇大摆的用户界面,这个url对我来说是开箱即用的:
http://localhost:8003/swagger-ui.html
检查您是否使用正确配置的url作为招摇规格。像http://localhost:8080/spring-安全rest/api/swagger ui/
没有工作,我得到了404。我也有同样的问题(springfox 3.0.0没有找到404)。通过将日志记录级别设置为“DEBUG”,我可以看到/v3/api文档的端点,它们工作正常,但“招摇过市ui”一点也没有
我最终发现,这表明:
3.0.0中的新url是/swagger ui/index.html或/swagger ui/而不是/swagger ui.html“
难道他们没有添加调试日志来指示Swigger UI的可用位置吗?请检查参考:
“2.1.3。从现有2.x版本迁移”
您可以从pom.xml中删除springfox-swagger 2和springfox swagger ui,并添加springfox启动程序(例如版本3.0.0)。此外,您还可以删除@EnableSwagger 2注释
http://localhost:8003/v2/api-docs
http://localhost:8003/swagger-ui.html