【问题标题】:Spring Webflux Router Functions not being used未使用 Spring Webflux 路由器功能
【发布时间】:2018-09-22 09:24:24
【问题描述】:

我有以下配置

@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 信息 54617 --- [主要] o.s.w.r.f.s.s.RouterFunctionMapping : 映射 /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

我在这里做错了什么?

【问题讨论】:

  • 原来RouterFunction和RequestMapping目前并不能很好的配合使用。我启用了执行器端点。希望 Spring 开发者尽快提供兼容层。
  • 不,他们有。看来您正在混合使用 Spring MVC 和 Spring WebFlux - 这是您面临问题的主要原因。

标签: java spring spring-boot spring-webflux


【解决方案1】:

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

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

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

【讨论】:

【解决方案2】:

我项目中的RouterFunction 也不起作用。然后我删除了spring-boot-starter-web 依赖,它开始工作了。

依赖项如下:

dependencies {
  // add starter-web & starter-webflux at same time,RouterFunction is not working
  // implementation 'org.springframework.boot:spring-boot-starter-web'
  implementation 'org.springframework.boot:spring-boot-starter-webflux'
  compileOnly 'org.projectlombok:lombok'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
  testImplementation 'io.projectreactor:reactor-test'
}

【讨论】:

    【解决方案3】:

    从starter-web 中排除starter-tomcat 依赖项,spring boot 将自动使用作为starter-webflux 一部分的Netty 服务器。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <groupId>org.springframework.boot</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2019-01-06
      相关资源
      最近更新 更多