[英]404 error with swagger-ui and spring webflux
I am developping REST services with Spring Webflux and I want to produce documentation with Swagger2 for my API with.我正在使用 Spring Webflux 开发 REST 服务,并且我想使用 Swagger2 为我的 API 生成文档。 I discovered that Webflux is only supported with Swagger2 version 3.0.0 snapshot.
我发现只有 Swagger2 版本 3.0.0 快照支持 Webflux。
Here's my configuration :这是我的配置:
My SwaggerConfiguration bean looks like this我的 SwaggerConfiguration bean 看起来像这样
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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.EnableSwagger2WebFlux;
@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.description("My Reactive API")
.title("My Domain object API")
.version("1.0.0")
.build())
.enable(true)
.select()
.apis(RequestHandlerSelectors.basePackage("com.mypackage.service.myobject.controller"))
.paths(PathSelectors.any())
.build();
}
}
My springboot app is defined like this :我的 springboot 应用程序定义如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.reactive.config.EnableWebFlux;
@SpringBootApplication
@EnableWebFlux
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Here's my pom configuration这是我的 pom 配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<properties>
<swagger.version>3.0.0-SNAPSHOT</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-webflux</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-integration-webflux</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
However, I get a 404 error when I type http://localhost:8080/swagger-ui.html in my browser.但是,当我在浏览器中键入http://localhost:8080/swagger-ui.html时,出现 404 错误。 If I type http://localhost:8080/v2/api-docs , I get the swagger document in json for my API.
如果我输入http://localhost:8080/v2/api-docs ,我会在 json 中为我的 API 获取 swagger 文档。
I run my springboot app in debug under Intellij.我在 Intellij 下的调试中运行我的 springboot 应用程序。 I try to run the fat jar in command line: same issue.
我尝试在命令行中运行 fat jar:同样的问题。 Does somebody know what is wrong with my configuration?
有人知道我的配置有什么问题吗?
Thanks谢谢
Found it!找到了!
I have to register a webflux resource handler for swagger-ui like this.我必须像这样为 swagger-ui 注册一个 webflux 资源处理程序。
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;
@Configuration
public class WebfluxConfig implements WebFluxConfigurer {
@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/");
}
}
Problem solved!问题解决了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.