WebFlux.fn 根本不路由 [英] WebFlux.fn doesn't route at all
问题描述
我正在尝试使用 WebFlux.fn.
I'm trying to use WebFlux.fn.
我编写了一个 Web 控制器并且它可以工作.
I wrote a Web controller and it works.
@RestController
@RequestMapping(path = "/api/v1/employees")
public class EmployeesController {
@RequestMapping(method = GET, path = "/{id:.+}", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Employee> read(
@PathVariable(name = "id) String id) {
}
@ResponseStatus(NO_CONTENT)
@RequestMapping(method = PUT, path = "/{id:.+}", consumes = APPLICATION_JSON_VALUE)
public void updated(@PathVariable(name = "id") String id,
@RequestBody Employee entity) {
}
@ResponseStatus(NO_CONTENT)
@RequestMapping(method = DELETE, path = "/{id:.+}")
public void delete(@PathVariable(name = "id") String id) {
}
}
而且我还写了一个 Reactive Web 控制器,它也能工作.
And I also wrote a Reactive Web controller and it also works.
@RestController
@RequestMapping(path = "/api/v1/employees/reactive")
public class EmployeesReactiveController {
@RequestMapping(method = GET, path = "/{id:.+"}, produces = APPLICATION_JSON_VALUE)
public Mono<Employee> read(
@PathVariable(name = "id") String id) {
}
@ResponseStatus(NO_CONTENT)
@RequestMapping(method = PUT, path = "/{id:.+}", consumes = APPLICATION_JSON_VALUE)
public Mono<Boolean> updated(
@PathVariable(name = PATH_NAME_EMPLOYEE_ID) String id,
@RequestBody Employee entity) {
}
@ResponseStatus(NO_CONTENT)
@RequestMapping(method = DELETE, path = "/{id:.+}")
public Mono<Long> delete(@PathVariable(name = "id") String id) {
}
}
我坚持使用 WebFlux.fn 版本.
And I'm stuck with an WebFlux.fn version.
@Configuration
public class EmployeesReactiveFnConfig {
@Bean
public RouterFunction<ServerResponse> employeesRouterFunction(
EmployeesReactiveFnHandler handler) {
return route(GET("/api/v1/employees/reactive/fn/{id:.+}"), handler::read);
}
}
@Component
public class EmployeesReactiveFnHandler {
public Mono<ServerResponse> read(@NotBlank String id) {
}
public Mono<ServerResponse> read(ServerRequest request) {
return read(request.pathVariable("id"));
}
}
这两个类似乎被Spring识别了.
These two classes seem to be recognized by Spring.
...DefaultListableBeanFactory - Creating shared instance of singleton bean 'employeesReactiveFnConfig'
...DefaultListableBeanFactory - Creating shared instance of singleton bean 'employeesReactiveFnHandler'
每当我尝试时,我都得到了这个.
Whenever I tried I got this.
o.s.s.w.u.m.AntPathRequestMatcher - Request '/api/v1/employees/reactive/fn/......' matched by universal pattern '/**'
o.s.security.web.FilterChainProxy - /api/v1/employees/reactive/fn/...... has an empty filter list
o.s.web.servlet.DispatcherServlet - GET "/api/v1/employees/reactive/fn/......", parameters={}
o.s.w.s.h.SimpleUrlHandlerMapping - Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
o.s.w.s.r.ResourceHttpRequestHandler - Resource not found
o.s.web.servlet.DispatcherServlet - Completed 404 NOT_FOUND
o.s.s.w.u.m.AntPathRequestMatcher - Request '/error' matched by universal pattern '/**'
o.s.security.web.FilterChainProxy - /error has an empty filter list
o.s.web.servlet.DispatcherServlet - "ERROR" dispatch for GET "/error", parameters={}
o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
o.s.w.s.m.m.a.HttpEntityMethodProcessor - Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
o.s.w.s.m.m.a.HttpEntityMethodProcessor - Writing [{timestamp=Fri Mar 20 18:54:24 KST 2020, status=404, error=Not Found, message=No message available, (truncated)...]
o.s.web.servlet.DispatcherServlet - Exiting from "ERROR" dispatch, status 404
我当前的依赖树看起来像这样.(为简洁起见删除了一些)
My current dependency tree looks like this. (Some removed for brevity)
>mvn dependency:tree | grep springframework
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.2.5.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:2.2.5.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.5.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.2.5.RELEASE:compile
[INFO] | | \- org.springframework:spring-jdbc:jar:5.2.4.RELEASE:compile
[INFO] | \- org.springframework:spring-tx:jar:5.2.4.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:2.2.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:2.2.5.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-actuator:jar:2.2.5.RELEASE:compile
[INFO] | | +- org.springframework:spring-context:jar:5.2.4.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-aop:jar:2.2.5.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:5.2.4.RELEASE:compile
[INFO] | | \- org.springframework:spring-beans:jar:5.2.4.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.2.5.RELEASE:compile
[INFO] | +- org.springframework.data:spring-data-jpa:jar:2.2.5.RELEASE:compile
[INFO] | | +- org.springframework.data:spring-data-commons:jar:2.2.5.RELEASE:compile
[INFO] | | \- org.springframework:spring-orm:jar:5.2.4.RELEASE:compile
[INFO] | \- org.springframework:spring-aspects:jar:5.2.4.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-redis:jar:2.2.5.RELEASE:compile
[INFO] | +- org.springframework.data:spring-data-redis:jar:2.2.5.RELEASE:compile
[INFO] | | +- org.springframework.data:spring-data-keyvalue:jar:2.2.5.RELEASE:compile
[INFO] | | +- org.springframework:spring-oxm:jar:5.2.4.RELEASE:compile
[INFO] | | \- org.springframework:spring-context-support:jar:5.2.4.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-redis-reactive:jar:2.2.5.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-hateoas:jar:2.2.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-web:jar:2.2.5.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.2.5.RELEASE:compile
[INFO] | | \- org.springframework:spring-webmvc:jar:5.2.4.RELEASE:compile
[INFO] | \- org.springframework.hateoas:spring-hateoas:jar:1.0.3.RELEASE:compile
[INFO] | \- org.springframework.plugin:spring-plugin-core:jar:2.0.0.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-security:jar:2.2.5.RELEASE:compile
[INFO] | +- org.springframework.security:spring-security-config:jar:5.2.2.RELEASE:compile
[INFO] | \- org.springframework.security:spring-security-web:jar:5.2.2.RELEASE:compile
[INFO] | \- org.springframework:spring-expression:jar:5.2.3.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.2.5.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test:jar:2.2.5.RELEASE:test
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.2.5.RELEASE:test
[INFO] | +- org.springframework:spring-core:jar:5.2.4.RELEASE:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.2.4.RELEASE:compile
[INFO] | +- org.springframework:spring-test:jar:5.2.4.RELEASE:test
[INFO] +- org.springframework.boot:spring-boot-starter-webflux:jar:2.2.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.2.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-reactor-netty:jar:2.2.5.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-validation:jar:2.2.5.RELEASE:compile
[INFO] | +- org.springframework:spring-web:jar:5.2.4.RELEASE:compile
[INFO] | +- org.springframework:spring-webflux:jar:5.2.4.RELEASE:compile
[INFO] +- org.springframework.data:spring-data-envers:jar:2.2.5.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-test:jar:5.3.0.RELEASE:test
[INFO] | \- org.springframework.security:spring-security-core:jar:5.3.0.RELEASE:compile
[INFO] | \- org.springframework.boot:spring-boot-starter-data-rest:jar:2.2.4.RELEASE:runtime
[INFO] | \- org.springframework.data:spring-data-rest-webmvc:jar:3.2.4.RELEASE:runtime
[INFO] | \- org.springframework.data:spring-data-rest-core:jar:3.2.4.RELEASE:runtime
推荐答案
您的应用程序不是 WebFlux 应用程序 - HATEOAS 依赖项正在传递 Spring MVC.
Your application is not a WebFlux application - the HATEOAS dependency is bringing Spring MVC transitively.
如果不使用这个依赖,你可以删除它(Spring HATOAS 在最新版本中也支持 WebFlux,但是 自动配置尚未完成).您还可以 强制使用 Spring Boot 中的 Web 应用程序类型,使用 spring.main.web-application-type
配置属性.
You can remove this dependency if it's not used (Spring HATOAS also supports WebFlux in recent version, but the auto-configuration is not complete yet). You can also force the type of web application in Spring Boot, with the spring.main.web-application-type
configuration property.
这篇关于WebFlux.fn 根本不路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!