Spring Webflux路由器功能未被使用

时间:2018-04-12 09:27:55

标签: 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 --- [主要]   o.s.w.r.f.s.s.RouterFunctionMapping:Mapped / user - &gt;   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:绑定请求上下文   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:查找处理程序方法   for path / user 2018-04-12 12:33:03.952 DEBUG 67056 ---   [nio-8080-exec-2] s.b.a.e.w.s.WebMvcEndpointHandlerMapping:没有   查找[/ user]的处理程序方法2018-04-12 12:33:03.952 DEBUG 67056   --- [nio-8080-exec-2] a.e.w.s.ControllerEndpointHandlerMapping:查找路径/用户的处理程序方法2018-04-12 12:33:03.952 DEBUG   67056 --- [nio-8080-exec-2] a.e.w.s.ControllerEndpointHandlerMapping:   没找到[/ user]的处理方法2018-04-12 12:33:03.952 DEBUG   67056 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping:   查找路径/用户的处理程序方法2018-04-12 12:33:03.953 DEBUG   67056 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping:   没有找到[/ user]的处理方法2018-04-12 12:33:03.953 DEBUG   67056 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping:   请求[/ user]的匹配模式为[/ ] 2018-04-12   12:33:03.953 DEBUG 67056 --- [nio-8080-exec-2]   o.s.w.s.handler.SimpleUrlHandlerMapping:URI模板变量   请求[/ user]是{} 2018-04-12 12:33:03.953 DEBUG 67056 ---   [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping:Mapping   [/ user]到处理程序的HandlerExecutionChain   [ResourceHttpRequestHandler [locations = [类路径资源   [META-INF / resources /],类路径资源[resources /],类路径   resource [static /],类路径资源[public /],ServletContext   资源[/]],   解析器= [org.springframework.web.servlet.resource.PathResourceResolver@7c4ca87c]]]   和1拦截器2018-04-12 12:33:03.954 DEBUG 67056 ---   [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet:   DispatcherServlet,名称为&#39; dispatcherServlet&#39;处理GET请求   [/ user] 2018-04-12 12:33:03.954 DEBUG 67056 --- [nio-8080-exec-2]   s.b.a.e.w.s.WebMvcEndpointHandlerMapping:查找处理程序方法   for path / user 2018-04-12 12:33:03.955 DEBUG 67056 ---   [nio-8080-exec-2] s.b.a.e.w.s.WebMvcEndpointHandlerMapping:没有   查找[/ user]的处理程序方法2018-04-12 12:33:03.955 DEBUG 67056   --- [nio-8080-exec-2] a.e.w.s.ControllerEndpointHandlerMapping:查找路径/用户的处理程序方法2018-04-12 12:33:03.955 DEBUG   67056 --- [nio-8080-exec-2] a.e.w.s.ControllerEndpointHandlerMapping:   没找到[/ user]的处理方法2018-04-12 12:33:03.955 DEBUG   67056 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping:   查找路径/用户的处理程序方法2018-04-12 12:33:03.956 DEBUG   67056 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping:   没找到[/ user]的处理方法2018-04-12 12:33:03.956 DEBUG   67056 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping:   请求[/ user]的匹配模式为[/ ] 2018-04-12   12:33:03.956 DEBUG 67056 --- [nio-8080-exec-2]   o.s.w.s.handler.SimpleUrlHandlerMapping:URI模板变量   请求[/ user]是{} 2018-04-12 12:33:03.956 DEBUG 67056 ---   [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping:Mapping   [/ user]到处理程序的HandlerExecutionChain   [ResourceHttpRequestHandler [locations = [类路径资源   [META-INF / resources /],类路径资源[resources /],类路径   resource [static /],类路径资源[public /],ServletContext   资源[/]],   解析器= [org.springframework.web.servlet.resource.PathResourceResolver@7c4ca87c]]]   和1拦截器2018-04-12 12:33:03.957 DEBUG 67056 ---   [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet:   [/ user]的Last-Modified值为:-1 2018-04-12 12:33:03.958 DEBUG   67056 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet:   Null ModelAndView返回带有名称的DispatcherServlet   &#39; dispatcherServlet&#39;:假设HandlerAdapter已完成请求   处理2018-04-12 12:33:03.958 DEBUG 67056 --- [nio-8080-exec-2]   o.s.web.servlet.DispatcherServlet:已成功完成   要求2018-04-12 12:33:03.960 DEBUG 67056 --- [nio-8080-exec-2]   o.s.b.w.s.f.OrderedRequestContextFilter:清除线程绑定   请求上下文: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   &#39; DispatcherServlet的&#39;处理[/ error] 2018-04-12的GET请求   12:33:03.960 DEBUG 67056 --- [nio-8080-exec-2]   s.b.a.e.w.s.WebMvcEndpointHandlerMapping:查找处理程序方法   for path / error 2018-04-12 12:33:03.961 DEBUG 67056 ---   [nio-8080-exec-2] s.b.a.e.w.s.WebMvcEndpointHandlerMapping:没有   找到[/ error]的处理程序方法2018-04-12 12:33:03.961 DEBUG 67056   --- [nio-8080-exec-2] a.e.w.s.ControllerEndpointHandlerMapping:查找路径/错误的处理程序方法2018-04-12 12:33:03.961   DEBUG 67056 --- [nio-8080-exec-2]   a.e.w.s.ControllerEndpointHandlerMapping:没有找到处理程序方法   for [/ error] 2018-04-12 12:33:03.962 DEBUG 67056 --- [nio-8080-exec-2]   s.w.s.m.m.a.RequestMappingHandlerMapping:查找处理程序方法   for path / error 2018-04-12 12:33:03.962 DEBUG 67056 ---   [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping:返回   处理方法[公共   org.springframework.http.ResponseEntity&GT;   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:返回缓存的实例   singleton bean&#39; basicErrorController&#39; 2018-04-12 12:33:03.962调试   67056 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet:   [/ error]的Last-Modified值为:-1 2018-04-12 12:33:03.966 DEBUG   67056 --- [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor:   写[{timestamp = Thu Apr 12 12:33:03 EEST 2018,status = 404,   error = Not Found,message =没有可用消息,path = / user}] as   &#34;应用/ JSON&#34;运用   [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   到DispatcherServlet的名称&#39; dispatcherServlet&#39;:假设   HandlerAdapter完成了请求处理2018-04-12 12:33:03.966   DEBUG 67056 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet
  :已成功完成请求

我在这里做错了什么?

3 个答案:

答案 0 :(得分:1)

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

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

此外,由于您正在使用Spring Boot,因此您不应添加@EnableWebFlux,因为它表示您希望完全控制WebFlux配置并手动执行Spring Boot。

答案 1 :(得分:1)

项目中的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'
}

答案 2 :(得分:1)

starter-tomcat中排除starter-web依赖性,spring boot将自动使用Netty服务器,该服务器是starter-webflux的一部分。

<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>