How Spring Boot uses @RequestParam for data validation
In web applications, user-submitted data is usually passed in the form of request parameters. In Spring Boot, @RequestParam
annotations to obtain request parameters. But how do you ensure the validity of these request parameters? In this article, we will introduce how to use @RequestParam
annotations for data validation, and provide some sample codes to help you better understand.
Introduction to @RequestParam
In Spring Boot, @RequestParam
annotations are used to get parameter values from the request. By default, parameters are required, and MissingServletRequestParameterException
an exception . However, you can use required
the attribute to make a parameter optional.
For example, the following code uses @RequestParam
annotations to obtain id
a request parameter named :
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id, @RequestParam Long status) {
// 处理获取用户逻辑
}
In the code above, @RequestParam
the annotation is used to get the request parameter status
named . An exception is thrown if the request does not include status
the parameter MissingServletRequestParameterException
.
Data validation
To ensure the validity of the request parameters, you can use the annotations in javax.validation.constraints
the package to verify the request parameters. For example, the following code uses @Min
annotations to validate id
the minimum value of the parameter:
@GetMapping("/users/{id}")
public User getUser(@PathVariable @Min(1) Long id) {
// 处理获取用户逻辑
}
In the above code, @Min
the annotation is used to verify that the minimum value of id
the parameter is 1. If id
the argument is less than 1, MethodArgumentNotValidException
an exception will be thrown.
In addition to @Min
annotations , there are many other annotations that can be used to validate request parameters. For example, @Max
annotations are used to verify the maximum value of a parameter, @NotBlank
annotations are used to verify whether a string is not empty, @Email
annotations are used to verify the format of an email address, etc.
custom error message
By default, MethodArgumentNotValidException
an exception . However, you can use message
the attribute to specify a custom error message.
For example, the following code uses a custom error message to validate the minimum value of id
the parameter :
@GetMapping("/users/{id}")
public User getUser(@PathVariable @Min(value = 1, message = "id 必须大于等于 1") Long id) {
// 处理获取用户逻辑
}
In the code above, we use message
the attribute to specify a custom error message. If id
the argument is less than 1, MethodArgumentNotValidException
an exception will be thrown with a custom error message.
Combined check
Sometimes, you may need to perform combined validation on multiple request parameters. For example, you might need to verify that start
the parameter is less than end
the parameter. In this case, you can use @Valid
annotations and custom objects to implement combined validation.
Here is a sample code that uses a custom object DateRange
to combine the checksum start
and end
parameters :
public class DateRange {
@NotNull
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate start;
@NotNull
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate end;
// getters/setters omitted
}
@GetMapping("/users")
public List<User> getUsers(@Valid DateRange dateRange) {
// 处理获取用户列表逻辑
}
In the code above, we use @Valid
annotations to validate DateRange
the object. DateRange
The object contains start
and end
properties, @NotNull
which @DateTimeFormat
are validated using the and annotations, respectively. If start
greater than end
, MethodArgumentNotValidException
an exception .
Summarize
In this article, we introduced how to use @RequestParam
annotations to obtain request parameters, and use javax.validation.constraints
annotations in the package to verify request parameters. We also show how to increase the flexibility of data validation by using custom error messages and combined validations. Through these sample codes, you can better understand how to use @RequestParam
for .
Here is the complete sample code:
@RestController
public class UserController {
@GetMapping("/users/{id}")
public User getUser(@PathVariable @Min(value = 1, message = "id 必须大于等于 1") Long id) {
// 处理获取用户逻辑
}
public class DateRange {
@NotNull
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate start;
@NotNull
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate end;
// getters/setters omitted
}
@GetMapping("/users")
public List<User> getUsers(@Valid DateRange dateRange) {
// 处理获取用户列表逻辑
}
}
Hope this article is helpful to you, thanks for reading!