I'm using WebFlux for Web development. And I find 2 ways to express request mappings.
@Controller
or @RestController
.RouterFunction
: RouterFunction
is used to route requests to a HandlerFunction
.I find the second harder to use than the first and also I do not know the difference between them.
So I wanted to ask which performs better 2 or 1 ?
Thanks my friends!
In a modern framework a router defines a direct connection between a "kind" of possible requests and its processor. By contrast, a controller gets just identifying information, and parses this data in its own context.
Techopedia Explains Route Control A routing control mechanism is composed of hardware and software, which monitors all the outgoing traffic through its connection with the Internet service providers (ISPs), and helps in selecting the best path for efficient delivery of the data.
@RestController annotation indicates that class is a controller where @RequestMapping methods assume @ResponseBody semantics by default. In @Controller, we need to use @ResponseBody on every handler method. In @RestController, we don't need to use @ResponseBody on every handler method.
Routers are a crucial element in many messaging architectures. They consume messages from a message channel and forward each consumed message to one or more different message channels depending on a set of conditions. Spring Integration provides the following routers: Payload Type Router. Header Value Router.
Let me start with your last question
Is 2 better than 1, in performance?
No, there is no difference in the performance.
I find that 2 is harder to use than 1
That is absolutely based on individual preference. May be you are used to imperative style of programming. And hence you feel it easy to write(Trust me after a while you would feel the same with Router Functions as well)
Basically Router Functions are one step towards functional style of programming. Spring wanted users to have the flexibility to configure routes in functional style.
Apart from this there is no difference between Controllers and Router Functions.
The RouterFunction has a similar purpose as the annotation. However, there is an important distinction: with the annotation, your route is limited to what can be expressed through the annotation values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With