JSR-303
Jsr-303 is a sub-specification in JAVA EE 6 called Bean Validation, Hibernate Validator is a reference implementation for Bean Validation. Hibernate Validator provides all of the built-in implementations of the JSR 303 specification. There are additional constraints.
The constraint is built into the Bean Validation
case
Entity class
public class User{
@NotNull
@Length(min = 1,max = 5)
private String name;
@Size(min=1,max=5)
private int pasWord
}
Copy the code
The controller
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
ResponseEntity createUser(@ModelAttribute @Valid User user, BindingResult result) {
if (result.hasErrors()) {
return. ; }return. ; }Copy the code
Hibernate Validate Hibernate Validate Hibernate Validate Hibernate Validate Hibernate Validate Hibernate Validate Hibernate Validate Hibernate Validate Hibernate Validate Hibernate Validate Hibernate Hibernate Validate Hibernate Hibernate Validate Hibernate Hibernate Validate Hibernate Hibernate Validate Hibernate Hibernate Validate Hibernate Hibernate Validate JSR-303 Hibernate Validate Hibernate Hibernate Validate jSR-303 Hibernate Validate Hibernate Hibernate Validate JSR-303 To support primitive type parameters, use the @Validated annotation of the Spring Validator.
@RestController
@RequestMapping("hello")
@Validated
public class HelloController{
@RequestMapping("get/{id}")
public String getById(@PathVariable @Min(1) int id) {
return "input id is "+ id ; }}Copy the code