使用的类javax.validation
导入的包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>xxxx.RELEASE</version>
</dependency>
通过springboot的其他包已经依赖hibernate-validator
,一般是spring-boot-starter-web
或者单独导入spring-boot-starter-validation
也可以。不能单独导入hibernate-validator
,会报错
自定义校验类
import com.haier.boot.valid.enums.ValidListEnum;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;
/**
* ValidListEnum: 校验的注解
* Object:校验对象属性类型 Object表示所有类型都可以
*/
public class ValidListValidator implements ConstraintValidator<ValidListEnum, Object> {
private List<String> codes;
@Override
public void initialize(ValidListEnum constraintAnnotation) {
codes = Arrays.asList(constraintAnnotation.list().split(","));
}
/**
* obj 调接口时传的实参
*/
@Override
public boolean isValid(Object obj, ConstraintValidatorContext constraintValidatorContext) {
if (obj == null || codes == null || codes.size() == 0) {
return true;
}
return codes.contains(obj.toString());
}
}
自定义注解
import com.haier.boot.valid.validator.ValidListValidator;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Documented
// 导入校验类
@Constraint(validatedBy = ValidListValidator.class)
public @interface ValidListEnum {
/**
* @return class containing enum values to which this String should match
*/
String list() default "";
/**
* @return the error message template
*/
String message() default "com.haier.hrwms.common.annotation.ValueOfEnum.message";
/**
* @return the groups the constraint belongs to
*/
Class<?>[] groups() default {};
// 负载
Class<? extends Payload>[] payload() default {};
}
全局异常处理(获取所有校验不通过的参数)
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public String methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
log.error("入参异常", e);
BindingResult bindingResult = e.getBindingResult();
StringBuilder errMsgStringBuilder = new StringBuilder("参数校验失败:");
for (FieldError fieldError : bindingResult.getFieldErrors()) {
errMsgStringBuilder.append(String.format("%s.%n", fieldError.getDefaultMessage()));
}
return errMsgStringBuilder.toString();
}
}
测试
import javax.validation.Valid;
@PostMapping("/export/list/testUser")
public void testUser(@RequestBody @Valid Person testUser) {
helloWorldService.testUser(testUser);
}
标签:lang,java,参字段,自定义,校验,import,validation,annotation
From: https://www.cnblogs.com/shareToAll/p/17965240