@Controller
Applied at the class level, @Controller marks a class as a Spring MVC controller and registers it as a Spring bean. The DispatcherServlet automatically detects such classes and routes incoming HTTP requests to methods annotated with @RequestMapping. This annotation is specifically intended for web controllers—use @Component or other stereotypes for non-web beans.
@RequestMapping
This annotation maps HTTP requests to handler methods or entire controller classes. When placed on a class, it defines a base path; method-level @RequestMapping annotations extend this base. It supports binding HttpServletRequest and HttpServletResponse, and allows configuration of media types via produces and consumes attributes.
@RestController
A convenience annotation that combines @Controller and @ResponseBody. It simplifies the creation of RESTful endpoints by ensuring return values are serialized directly into the HTTP response body (e.g., as JSON or XML), eliminating the need to annotate every method with @ResponseBody.
@Autowired
Provided by Spring (org.springframework.beans.factory.annotation.Autowired), this annotation injects dependencies by type. By default, it requires the dependency to exist; setting required = false permits null injection. To resolve ambiguity when multiple beans of the same type exist, pair it with @Qualifier to specify the bean name:
@Autowired
@Qualifier("userDao")
private UserDao userDao;
Injection can occur on fields or setter methods.
@Resource
Part of the JSR-250 standard (javax.annotation.Resource), @Resource defaults to injection by name. It supports two attributes: name (for bean name) and type (for bean type). If neither is specified, Spring attempts injection by field name:
@Resource(name = "userDao")
private UserDao userDao;
While usable on fields, applying it on setter methods aligns better with encapsulation principles.
@PathVariable
Extracts values from URI template variables. For example, in /user/{userId}, {userId} is bound to a method parameter annotated with @PathVariable("userId"). Regular expressions can also constrain path segments:
@RequestMapping("/javabeat/{regexp1:[a-z-]+}")
public String getRegExp(@PathVariable("regexp1") String part) {
// ...
}
@CookieValue
Binds the value of an HTTP cookie to a method parameter. Supports value (cookie name), required, and defaultValue:
@RequestMapping("/testCookieValue")
public String handle(@CookieValue("JSESSIONID") String sessionId) {
System.out.println("Session ID: " + sessionId);
return "success";
}
@RequestParam
Maps query or form parameters to method arguments. The value attribute specifies the parameter name:
@RequestMapping("/testRequestParam")
public String fetch(@RequestParam("id") int identifier) {
System.out.println("ID: " + identifier);
return "success";
}
Additional attributes include required and defaultValue.
@ResponseBody
Indicates that a method’s return value should be written directly to the HTTP response body, typically serialized as JSON or XML using configured HttpMessageConverter instances. Commonly used in REST APIs where view are not rendered.