Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Router and Annotated Controllers?

I'm using WebFlux for Web development. And I find 2 ways to express request mappings.

  1. Annotated Controllers: Like @Controller or @RestController.
  2. 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!

like image 422
MrXionGe Avatar asked Aug 10 '18 12:08

MrXionGe


People also ask

What is the difference between controller and router?

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.

What is router controller?

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.

What is the difference between controller and rest controller annotation?

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

What is router in spring?

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.


2 Answers

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.

like image 93
pvpkiran Avatar answered Oct 13 '22 14:10

pvpkiran


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.

like image 31
nairavs Avatar answered Oct 13 '22 13:10

nairavs