美文网首页
springboot中关于自定义注解校验

springboot中关于自定义注解校验

作者: Lolipiza | 来源:发表于2020-12-28 16:14 被阅读0次

说明:本文分两部分,第一部分是普通的字段校验;第二部分是对类的自定义校验,并自定义校验失败返回default message,用于根据入参判断不同校验条件,如当入参int1为0时,int2必须为null;当int1为1时,int2必须不为null。这类复杂校验无法使用简单分组校验实现。

1.普通字段自定义注解校验

1.例如我想要校验某个入参是否是json字符串,假设我的自定义注解为@JSONStringAnnotation,并在某个bean中的customFields字段使用:

    @JSONStringAnnotation
    private String customFields;

2.新建注解接口类:

@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.FIELD})
@Documented
@Constraint(validatedBy = JSONStringValidator.class)
public @interface JSONStringAnnotation {
    String message() default "字符串不符合json格式";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};

}

3.新建具体的校验类:

public class JSONStringValidator implements ConstraintValidator<JSONStringAnnotation, String> {

    @Override
    public void initialize(JSONStringAnnotation constraintAnnotation) {

    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        try {
            JSON.parseObject(value);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}

以上就完成了对一个字段的自定义注解校验,是不是很简单,下面我们来看一下对类进行自定义校验,其实大部分也相同

2.对类自定义注解校验,其实步骤和上面差不多

1.将自定义注解加在需要校验的类上:

@Data
@DIYAnnotation
public class TestValid {
    // 当int1为0时,int2必须为null;当int1为1时,int2必须不为null;
    private Integer int1;
    private Integer int2;
}

2.注解接口类

@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE})
@Documented
@Constraint(validatedBy = DIYFildValidator.class)
public @interface DIYAnnotation {
    String message() default "!!!!!!No";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

注意此注解是对类使用,@Target需要使用ElementType.TYPE
3.具体校验类

public class DIYFildValidator implements ConstraintValidator<DIYAnnotation, TestValid> {
    @Override
    public void initialize(DIYAnnotation constraintAnnotation) {
    }
    @Override
    public boolean isValid(TestValid testValid, ConstraintValidatorContext context) {
        if(testValid.getInt1() == 0 && nonNull(testValid.getInt2())) {
            return diyErrMsg(context, "int1为0时,int2必须为null");
        }
        if (testValid.getInt1() == 1 && isNull(testValid.getInt2())) {
            return false;
        }
        return true;
    }
    // 自定义message
    private boolean diyErrMsg(ConstraintValidatorContext context, String message) {
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
        return false;
    }
}

以上就完成了自定义的类校验,别忘了在controller方法上加上@Valid
public int test(@RequestBody @Valid TestValid testValid)

捕获MethodArgumentNotValidException异常,获取错误信息,封装返回

@ExceptionHandler(value = {MethodArgumentNotValidException.class})
    public @ResponseBody
    ResponseObject handleValidException(MethodArgumentNotValidException ex, NativeWebRequest request) {
        BindingResult result = ex.getBindingResult();
        List<FieldError> fieldErrorList = result.getFieldErrors();
        List<String> filedErrorInfo = new ArrayList<String>();
        String message = null;
        HttpServletRequest requestHttp = request.getNativeRequest(HttpServletRequest.class);
        for (FieldError filedError : fieldErrorList) {
            filedErrorInfo.add(filedError.getField());
            message = filedError.getDefaultMessage();
            log.error("/data/controller 参数校验失败 路径:{},错误信息:{}", requestHttp.getServletPath(), message);
            break;
        }
        return ResponseObject.getResponseObject(BaseCodeType.PARAM_FAIL, message);
    }

相关文章

网友评论

      本文标题:springboot中关于自定义注解校验

      本文链接:https://www.haomeiwen.com/subject/diqtoktx.html