http://localhost:8080/MyWebApp/users?page=2&size=10&sort=name
As you can see, there are 3 query parameters in this request: page, size and name. Their values are 2, 10 and “name”, respectively. With Spring MVC, you can bind these request parameters to method parameters in a controller class as follows:package net.codejava;
...
@Controller
public class UserController {
@GetMapping("/users")
public String listUsers(Integer page, Integer size, String sort) {
...
}
}In this example, the listUsers() handler method declares 3 parameters: page, size and name, which are bound to the corresponding parameters in the request URL. Note that the method parameter names are identical to the request parameter names.This is a sensible default in Spring MVC: it automatically binds request parameters of simple types (String, Integer, Long, etc.) to method parameters. This default behavior is both useful and convenient for developers, isn’t it?And you know, in this automatic binding, method parameters will have a null value if the corresponding request parameters are missing. In other words, request parameters are optional and default to null when not provided.But what if you want to specify different default values? Or if a parameter is required? Or if you need to use different binding name? In these cases, you need to use the @RequestParam annotation.@GetMapping("/users")
public String listUsers(@RequestParam(defaultValue = "1") Integer page,
Integer size, String sort) {
...
}In this case, the page parameter will default to a value of 1 if it is missing from the request URL:http://localhost:8080/MyWebApp/users?size=10&sort=name
Note that when the defaultValue attribute is used, the parameter becomes optional, as it automatically sets the required flag to false.@GetMapping("/users")
public String listUsers(
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(required = false) Integer size,
@RequestParam(required = true) String sort) {
...
}This explicitly indicates that the size parameter is optional, while the sort parameter is mandatory. @GetMapping("/users")
public String listUsers(...
@RequestParam(name = "sort") String sortField) {
...
}This improves the readability of the code: the request parameter is named sort but it is bound to the method parameter named sortField.By combining all these options, you might end up with the following code:@GetMapping("/users")
public String listUsers(
@RequestParam(name = "page", defaultValue = "1", required = false) Integer pageNum,
@RequestParam(name = "size", defaultValue = "10", required = false) Integer pageSize,
@RequestParam(name = "sort", defaultValue = "name", required = false) String sortField) {
...
}This sets default values for each parameter, specifies different binding names, and makes all parameters optional. While it may be verbose, it improves code readability for developers.If you want to see the coding in action and how this annotation is used in a real-world project, this video will help you have better understanding:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.