Spring MVC’s @RestController
@RestController
is an annotation in Spring MVC used to create RESTful web services. It combines the @Controller
and @ResponseBody
annotations, simplifying the controller implementation. Here are some key annotations related to @RestController
along with their descriptions and usage:
@RestController
Description: Indicates that the class is a controller where every method returns a domain object instead of a view. Each method’s return value is automatically serialized to JSON and written into the HTTP response body.
Usage: Typically used to create REST APIs. Used at the class level.
1
2
3
4
public class MyController {
// ...
}
@RequestMapping
Description: Maps HTTP requests to handler methods of MVC and REST controllers.
Usage: Can be used at both the class and method levels. Supports configuring URL patterns, HTTP methods, request parameters, headers, etc.
1
2
3
4
public List<User> getUsers() {
// ...
}
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping
Description: Specialized versions of
@RequestMapping
for specific HTTP methods.Usage: Used at the method level, directly specifying the URL path and the HTTP method type.
1
2
3
4
public User getUser() {
// ...
}
@PathVariable
Description: Binds a method parameter to a URI template variable.
Usage: Commonly used in RESTful web services.
1
2
3
4
public User getUser( { Long id)
// ...
}
@RequestParam
Description: Binds a method parameter to a web request parameter.
Usage: Used to handle query parameters.
1
2
3
4
public List<User> getUsers( { String role)
// ...
}
@RequestBody
Description: Binds the HTTP request body to a method parameter.
Usage: Commonly used in POST and PUT methods.
1
2
3
4
public User addUser( { User user)
// ...
}
@ResponseBody
Description: Indicates that the return value of a method should be written directly to the HTTP response body.
Usage: In a
@RestController
, all methods implicitly use@ResponseBody
.1
2
3
4
public String greeting() {
return "Hello, World";
}
@ResponseStatus
Description: Marks a method or exception class with the status code and reason message that should be returned.
Usage: Often used for exception handling or specific operations.
1
2
3
4
5
public User addUser( { User user)
// ...
}
@CrossOrigin
Description: Enables cross-origin resource sharing (CORS) for the annotated methods or types.
Usage: Can be applied at both the class and method levels to allow cross-origin requests.
1
2
3
4
5
public List<User> getUsers() {
// ...
}
Example of a Spring MVC Controller with @RestController
1 | import org.springframework.web.bind.annotation.*; |
In this example:
@RestController
is used to define the controller.- Various HTTP methods (
@GetMapping
,@PostMapping
,@PutMapping
,@DeleteMapping
) are used to handle different types of HTTP requests. @RequestMapping("/api")
at the class level sets the base URL for all endpoints in the controller.@PathVariable
,@RequestParam
, and@RequestBody
are used to handle path variables, query parameters, and request bodies, respectively.@ResponseStatus
is used to set the HTTP status code for the response.