Spring boot development web projects sometimes need to perform some basic verification of the parameters passed by the controller layer, such as non-empty, range of integer values, length of strings, date, email, etc. Spring supports JSR-303 Bean Validation API, which can be easily checked.
Use annotations for verification
First define a form encapsulation object
class RequestForm { @Size(min = 1, max = 5) private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}The name field is limited to the length of 1 to 5 with Size annotation. Size is the constraint annotation in the javax.validation package.
When using the @Valid annotation, it means that the bean is to be verified.
@ResponseBody @GetMapping(value = "bean") public String validate(@Valid RequestForm request) { System.out.println(request.getName()); return "OK"; }Custom annotations
If the built-in annotations are not enough, you can customize the annotations.
For example, first define an annotation NameConstraint, and restrict the name field to be selected from specific data.
@Target({ ElementType.FIELD, ElementType.PARAMETER })@Retention(RetentionPolicy.RUNTIME)@Constraint(validatedBy = NameConstraintValidator.class)@interface NameConstraint { String[] allowedValues(); Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; String message();}Where allowedValues represents the legal value range, and message is the display information for verification failure.
Message, groups, and payload are fields required by hibernate validator. If you want to know, please refer to the official documentation.
Define a validator for real verification
class NameConstraintValidator implements ConstraintValidator<NameConstraint, String> { private String[] validValues; @Override public void initialize(NameConstraint constraintAnnotation) { validValues = constraintAnnotation.allowedValues(); } @Override public boolean isValid(String value, ConstraintValidatorContext context) { for (String s : this.validValues) { if (s.equals(value)) { return true; } } return false; }}Use in form bean as follows
class RequestFormWithCustomConstraint { @NameConstraint(allowedValues = { "bar", "foo" }, message = "Only bar,foo") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}Direct verification of parameters
What should I do if I don’t want to encapsulate an object? This parameter can be checked directly
@Controller@Validated@RequestMapping(value = "validator")public class ParameterValidatorDemoController { @ResponseBody @GetMapping(value = "simple") public String validateParameter(@Size(min = 1, max = 5) String name) { System.out.println(name); return "OK"; }}The @Validated annotation above the controller tells spring that it needs to scan this class to check the constraint annotation.
For details, please refer to the relevant chapters of the official document.
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#boot-features-validation
https://docs.spring.io/spring/docs/4.3.16.RELEASE/spring-framework-reference/htmlsingle/#validation-beanvalidation
Code on github
https://github.com/kabike/spring-boot-demo
Summarize
The above is a detailed explanation of the Spring boot parameter verification method introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply to you in time!