java - 未使用 Spring Webflux 路由器功能

标签 java spring spring-boot spring-webflux

我有以下配置

@Configuration
@EnableWebFlux
public class WebfluxConfig {

    @Bean
    RouterFunction<?> routerFunction(UserResource userResource) {
        return route(GET("/user"), r -> ok()
                .body(userResource.findAll(), UserDto.class));
    }
}

当我启动应用程序时,它会生成预期的映射

2018-04-12 12:12:06.682 INFO 54617 --- [ main] o.s.w.r.f.s.s.RouterFunctionMapping : Mapped /user -> c.n.a.i.c.WebfluxConfig$$Lambda$764/909878836@14ba7f15

然而,当我尝试连接到它时,我得到了 404。而且路由器谓词还没有被调用一次。

curl -v localhost:8080/user

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)

> GET /user HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>

< HTTP/1.1 404
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Thu, 12 Apr 2018 09:15:52 GMT
<

* Connection #0 to host localhost left intact
{"timestamp":"2018-04-12T09:15:52.475+0000","status":404,"error":"Not Found","message":"No message available","path":"/user"}%

这是来自服务器的日志

> 2018-04-12 12:33:03.950 DEBUG 67056 --- [nio-8080-exec-2]
> o.s.b.w.s.f.OrderedRequestContextFilter  : Bound request context to
> thread: org.apache.catalina.connector.RequestFacade@51160dcb
> 2018-04-12 12:33:03.950 DEBUG 67056 --- [nio-8080-exec-2]
> s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Looking up handler method
> for path /user 2018-04-12 12:33:03.952 DEBUG 67056 ---
> [nio-8080-exec-2] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Did not
> find handler method for [/user] 2018-04-12 12:33:03.952 DEBUG 67056
> --- [nio-8080-exec-2] a.e.w.s.ControllerEndpointHandlerMapping : Looking up handler method for path /user 2018-04-12 12:33:03.952 DEBUG
> 67056 --- [nio-8080-exec-2] a.e.w.s.ControllerEndpointHandlerMapping :
> Did not find handler method for [/user] 2018-04-12 12:33:03.952 DEBUG
> 67056 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping :
> Looking up handler method for path /user 2018-04-12 12:33:03.953 DEBUG
> 67056 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping :
> Did not find handler method for [/user] 2018-04-12 12:33:03.953 DEBUG
> 67056 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  :
> Matching patterns for request [/user] are [/**] 2018-04-12
> 12:33:03.953 DEBUG 67056 --- [nio-8080-exec-2]
> o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for
> request [/user] are {} 2018-04-12 12:33:03.953 DEBUG 67056 ---
> [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping
> [/user] to HandlerExecutionChain with handler
> [ResourceHttpRequestHandler [locations=[class path resource
> [META-INF/resources/], class path resource [resources/], class path
> resource [static/], class path resource [public/], ServletContext
> resource [/]],
> resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@7c4ca87c]]]
> and 1 interceptor 2018-04-12 12:33:03.954 DEBUG 67056 ---
> [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        :
> DispatcherServlet with name 'dispatcherServlet' processing GET request
> for [/user] 2018-04-12 12:33:03.954 DEBUG 67056 --- [nio-8080-exec-2]
> s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Looking up handler method
> for path /user 2018-04-12 12:33:03.955 DEBUG 67056 ---
> [nio-8080-exec-2] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Did not
> find handler method for [/user] 2018-04-12 12:33:03.955 DEBUG 67056
> --- [nio-8080-exec-2] a.e.w.s.ControllerEndpointHandlerMapping : Looking up handler method for path /user 2018-04-12 12:33:03.955 DEBUG
> 67056 --- [nio-8080-exec-2] a.e.w.s.ControllerEndpointHandlerMapping :
> Did not find handler method for [/user] 2018-04-12 12:33:03.955 DEBUG
> 67056 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping :
> Looking up handler method for path /user 2018-04-12 12:33:03.956 DEBUG
> 67056 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping :
> Did not find handler method for [/user] 2018-04-12 12:33:03.956 DEBUG
> 67056 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  :
> Matching patterns for request [/user] are [/**] 2018-04-12
> 12:33:03.956 DEBUG 67056 --- [nio-8080-exec-2]
> o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for
> request [/user] are {} 2018-04-12 12:33:03.956 DEBUG 67056 ---
> [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping
> [/user] to HandlerExecutionChain with handler
> [ResourceHttpRequestHandler [locations=[class path resource
> [META-INF/resources/], class path resource [resources/], class path
> resource [static/], class path resource [public/], ServletContext
> resource [/]],
> resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@7c4ca87c]]]
> and 1 interceptor 2018-04-12 12:33:03.957 DEBUG 67056 ---
> [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        :
> Last-Modified value for [/user] is: -1 2018-04-12 12:33:03.958 DEBUG
> 67056 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        :
> Null ModelAndView returned to DispatcherServlet with name
> 'dispatcherServlet': assuming HandlerAdapter completed request
> handling 2018-04-12 12:33:03.958 DEBUG 67056 --- [nio-8080-exec-2]
> o.s.web.servlet.DispatcherServlet        : Successfully completed
> request 2018-04-12 12:33:03.960 DEBUG 67056 --- [nio-8080-exec-2]
> o.s.b.w.s.f.OrderedRequestContextFilter  : Cleared thread-bound
> request context: org.apache.catalina.connector.RequestFacade@51160dcb
> 2018-04-12 12:33:03.960 DEBUG 67056 --- [nio-8080-exec-2]
> o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name
> 'dispatcherServlet' processing GET request for [/error] 2018-04-12
> 12:33:03.960 DEBUG 67056 --- [nio-8080-exec-2]
> s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Looking up handler method
> for path /error 2018-04-12 12:33:03.961 DEBUG 67056 ---
> [nio-8080-exec-2] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Did not
> find handler method for [/error] 2018-04-12 12:33:03.961 DEBUG 67056
> --- [nio-8080-exec-2] a.e.w.s.ControllerEndpointHandlerMapping : Looking up handler method for path /error 2018-04-12 12:33:03.961
> DEBUG 67056 --- [nio-8080-exec-2]
> a.e.w.s.ControllerEndpointHandlerMapping : Did not find handler method
> for [/error] 2018-04-12 12:33:03.962 DEBUG 67056 --- [nio-8080-exec-2]
> s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method
> for path /error 2018-04-12 12:33:03.962 DEBUG 67056 ---
> [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning
> handler method [public
> org.springframework.http.ResponseEntity<java.util.Map<java.lang.String,
> java.lang.Object>>
> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)]
> 2018-04-12 12:33:03.962 DEBUG 67056 --- [nio-8080-exec-2]
> o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance
> of singleton bean 'basicErrorController' 2018-04-12 12:33:03.962 DEBUG
> 67056 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        :
> Last-Modified value for [/error] is: -1 2018-04-12 12:33:03.966 DEBUG
> 67056 --- [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor  :
> Written [{timestamp=Thu Apr 12 12:33:03 EEST 2018, status=404,
> error=Not Found, message=No message available, path=/user}] as
> "application/json" using
> [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@1c00d406]
> 2018-04-12 12:33:03.966 DEBUG 67056 --- [nio-8080-exec-2]
> o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned
> to DispatcherServlet with name 'dispatcherServlet': assuming
> HandlerAdapter completed request handling 2018-04-12 12:33:03.966
> DEBUG 67056 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet   
> : Successfully completed request

我做错了什么?

最佳答案

看来您正在使用 Spring Boot,值得在您的问题中指出这一点(或相应地标记它)。

看起来您正在使用 Spring MVC(或者至少将 Spring MVC 作为传递依赖);首先,如果您希望使用 Spring WebFlux,则应删除该依赖项。

此外,由于您使用的是 Spring Boot,因此不应添加 @EnableWebFlux,因为它会向 Spring Boot 发出信号,表明您希望完全控制 WebFlux 配置并手动执行操作。

关于java - 未使用 Spring Webflux 路由器功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49792913/

相关文章:

java - DropWizard 和 Camel 集成

java - Selenium 获取文本

spring - Hipster 堆栈 (Yeoman + Maven + Spring) 错误

java - 如何在 Spring-WS 中向响应负载添加附件?

Spring Boot 内嵌Tomcat 启动失败

java - 如何将 Spring Boot 长转换为最新版本

java - Spring Integration 5.2.2.RELEASE - 使用 @IntegrationConverter 的集成流转换不适用于 jackson databind

java - 为什么用Java从网页中检索数据时html实体显示错误

java - 执行更新/删除查询;嵌套异常是 javax.persistence.TransactionRequiredException : Executing an update/delete query

java - 我如何知道选择哪个 Maven 依赖版本?