The primary annotation used to designate a controller class in frameworks like Spring MVC is @Controller
.
Understanding the @Controller
Annotation
The @Controller
annotation serves as a fundamental building block in web applications, particularly within the Spring Framework. It explicitly indicates that an annotated class functions as a "Controller." This type of class is responsible for handling incoming web requests, processing them, and ultimately preparing a suitable response, often involving interaction with service layers and data access components.
A key characteristic of @Controller
is that it is a specialization of the core Spring @Component
annotation. This means that, similar to other Spring-managed components, classes marked with @Controller
are automatically discovered and registered as beans in the application's context through classpath scanning. This automatic detection mechanism significantly simplifies the configuration process, allowing developers to focus more on business logic rather than boilerplate setup.
Key Characteristics and Usage
- Role Definition: Clearly defines a class's role in the MVC (Model-View-Controller) architecture, specifically for handling HTTP requests.
- Specialization: It's a meta-annotation that extends
@Component
, providing a more specific semantic meaning for web-tier components. - Autodetection: Enables Spring's component scanning feature to automatically find and configure controller beans without explicit XML or Java configuration entries.
- Request Mapping: Typically used in conjunction with other annotations such as
@RequestMapping
to define the specific URL patterns that a controller method will handle. - View Resolution: Controllers annotated with
@Controller
often return logical view names, which are then resolved by aViewResolver
to render a user interface (e.g., JSP, Thymeleaf, Freemarker).
Example of a Controller Class
Here's a simple example demonstrating the use of @Controller
in a Spring MVC application:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;
@Controller
public class HomeController {
@GetMapping("/hello")
public String sayHello(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "hello"; // This refers to a view named 'hello' (e.g., hello.html, hello.jsp)
}
@GetMapping("/")
public String homepage() {
return "index"; // Returns the 'index' view
}
}
In this example, HomeController
is marked as a @Controller
, making it discoverable by Spring. The methods sayHello
and homepage
are mapped to specific URL paths using @GetMapping
, a composed annotation for @RequestMapping(method = RequestMethod.GET)
.
@Controller
vs. @RestController
While @Controller
is foundational for web controllers, it's important to distinguish it from @RestController
, especially in modern web development that heavily relies on RESTful APIs.
Feature | @Controller |
@RestController |
---|---|---|
Primary Use | Traditional Spring MVC applications (serving views) | Building RESTful web services (serving data like JSON/XML) |
@ResponseBody |
Requires the explicit use of @ResponseBody annotation on methods to return data directly instead of a view name. |
Automatically includes the functionality of @ResponseBody for all handler methods within the class. |
Return Value | Typically returns a logical view name (e.g., "home"). | Directly returns the object that will be serialized into the HTTP response body (e.g., JSON, XML). |
Composition | A specialization of @Component . |
A convenience annotation that combines @Controller and @ResponseBody . |
For web applications primarily serving HTML pages, @Controller
is the appropriate choice. For building APIs that deliver data to client-side frameworks or other services, @RestController
is generally preferred due to its built-in @ResponseBody
behavior.