Junior — Middle
What is the role and purpose of different types of routers in Java?
sobes.tech AI
Answer from AI
In Java, the term "router" is not a standard concept of the language, but in the context of web applications and frameworks (such as Spring MVC, JAX-RS), routers are responsible for matching incoming HTTP requests with the corresponding handlers (controllers).
Different types of routers may include:
- Static routers — match fixed URL paths with handlers.
- Dynamic routers — support parameters in the path, for example
/user/{id}. - Filters and interceptors — intermediate handlers that can modify the request or response.
Overall, the role of a router is to direct requests to the appropriate components of the application, ensuring correct processing and logic.
Example using Spring MVC:
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User getUser(@PathVariable String id) {
// request processing
}
}
Here, Spring acts as a router, directing requests /users/{id} to the getUser method.