Ad

Spring Webflux Router Functions Not Being Used

I have the following configuration

@Configuration
@EnableWebFlux
public class WebfluxConfig {

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

When I start the application, it kind of making the expected mapping

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/[email protected]

However when I try to connect to it, I got 404. And the router predicate has not been invoked once.

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"}%

And here are the logs from server

> 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: [email protected]
> 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=[[email protected]4ca87c]]]
> 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=[[email protected]4ca87c]]]
> 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: [email protected]
> 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.springfr[email protected]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

What have I did wrong here?

Ad

Answer

It seems you're using Spring Boot, it's worth pointing this out in your question (or tagging it accordingly).

It looks like you're using Spring MVC (or at least bringing Spring MVC as a transitive dependency); first, you should remove that dependency if you wish to use Spring WebFlux.

Also, since you're using Spring Boot, you should not add @EnableWebFlux since it signals Spring Boot that you want to take full control over the WebFlux configuration and do things manually.

Ad
source: stackoverflow.com
Ad