Junior — Middle
What is the role of the @Controller annotation in the application structure?
sobes.tech AI
Answer from AI
The @Controller annotation in Java (most commonly in the Spring Framework) designates a class as a controller in the MVC (Model-View-Controller) architecture. This means that the class is responsible for handling HTTP requests, receiving data from the user, invoking business logic, and returning a response (such as a web page or JSON data).
Example:
@Controller
public class UserController {
@GetMapping("/users")
public String listUsers(Model model) {
model.addAttribute("users", userService.getAllUsers());
return "userList"; // view name
}
}
Here, @Controller allows Spring to detect this class and register it as a request handler.