In comparison to Spring MVC, the WebFlux RouterFunction builder does not raise an exception whereas Spring MVC throws a "java.lang.IllegalStateException" saying: "Ambiguous mapping. Cannot map '...Controller' method" on compile time.
It took me half an hour trying to figure out why my endpoint did not return any data, instead, it returned a 404 Not Found.
Example: In Spring MVC, this does not compile:
@GetMapping("/all")
public List<User> getAll() {
return ...
}
@GetMapping("/all")
public List<User> getAll() {
return ...
}
Meanwhile, the Spring WebFlux RouterFunctions builder lets you compile this (nor does it throw an exception on runtime):
@Bean
RouterFunction<ServerResponse> userRoutes(UserHandler handler) {
return RouterFunctions.route()
.GET("/all", handler::findAll)
.GET("/all", handler::findAll)
.build();
Comment From: bclozel
Hello @itsandreramon , I'm sorry you had a hard time tracking this problem down in your application.
When it comes to routing, the annotation and functional variants are quite different:
- annotations are all considered "at the same level", as they are added to a collection-like structure and ordered by their specificity. When two mappings within a single have the same value, we're in a position to catch duplicate routes
- on the other hand, functional routes are defined by an algorithm and it is expected to have duplicate mappings in the router - they're processed in order with the given predicates, which can be anything: testing a request header value, a property in the application, the time of day, etc.
With that last description, you can see how things can get quite complex quickly. The functional model is very efficient and flexible. Maybe a specific RouterFunctions.Visitor could help, but given the variety of use cases, I think we'd get a lot of false positives or we'd miss a lot of real cases. If you've got an idea about how to implement that, please share it with us!
Did you try debugging your RouterFunction to figure out what happened? Did you use Spring Boot actuator tools to list available routes? Maybe there's something else we could improve here.
Thanks!
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.