目录
- 1. jar包导入
- 2. 自定义注解
- 3. 校验类
- 4. 定义枚举类
- 5. 被校验的实体类
- 6. 全局异常拦截
- 7. controller
- 8. 测试
1. jar包导入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
2. 自定义注解
package com.example.server.annotation;
import com.example.server.valid.EnumValidator;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Constraint(validatedBy = EnumValidator.class)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface RegexValidation {
Class<? extends Enum<?>> value();
String message() default "Invalid input format";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
3. 校验类
package com.example.server.valid;
import com.example.server.annotation.RegexValidation;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
public class EnumValidator implements ConstraintValidator<RegexValidation, String> {
private Class<? extends Enum<?>> enumClass;
@Override
public void initialize(RegexValidation constraintAnnotation) {
this.enumClass = constraintAnnotation.value();
}
@Override
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
if (value == null) {
return true;
}
for (Enum<?> e : enumClass.getEnumConstants()) {
if (e.name().equals(value)) {
return true;
}
}
return false;
}
}
4. 定义枚举类
package com.example.server.enums;
public enum GenderRegex {
MALE, FEMALE, OTHER;
}
5. 被校验的实体类
package com.example.server.entity;
import com.example.server.annotation.RegexValidation;
import com.example.server.enums.GenderRegex;
import lombok.Data;
@Data
public class User {
private String name;
private Integer age;
@RegexValidation(value = GenderRegex.class, message = "输入的性别不符合要求,请重新输入")
private String gender;
}
6. 全局异常拦截
package com.example.server.advice;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class ControllerExceptionAdvice {
@ExceptionHandler(MethodArgumentNotValidException.class)
public String methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
return e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
}
@ExceptionHandler(Exception.class)
public String exceptionHandler(Exception e) {
return e.getMessage();
}
}
7. controller
package com.example.server.controller;
import com.example.server.entity.User;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ServerController {
@PostMapping("/hello")
public User hello(@RequestBody @Valid User user) {
return user;
}
}
8. 测试