自定义JSR-330 注解校验器

2018-01-04  本文已影响54人  唐岭云

导入包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

书写自定义注解

package com.bioodas.seckill.annotation;

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.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
import com.bioodas.seckill.validator.IsMobileValidator;

/**
 * @author TangLingYun
 * @describe 参数校验器 字段是否是手机号码格式
 * @date 2018年1月3日
 */
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {IsMobileValidator.class })
public @interface IsMobile {
    
    boolean required() default true;
    
    String message() default "手机号码格式有误";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };

}

书写自定义实现

package com.bioodas.seckill.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang3.StringUtils;

import com.bioodas.seckill.annotation.IsMobile;
import com.bioodas.seckill.util.ValidatorUtil;

/**
 * @author TangLingYun
 * @describe 手机号码校验器具体实现
 * @date 2018年1月3日
 */
public class IsMobileValidator implements ConstraintValidator<IsMobile,String>{

    /**是否必传*/
    private boolean required = false;
    
    /**得到获取注解值 初始化个人参数*/
    @Override
    public void initialize(IsMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    /**是否通过*/
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(required) {
            return ValidatorUtil.isMobile(value);
        }else {
            if(StringUtils.isBlank(value)) {
                return true;
            }else {
                return ValidatorUtil.isMobile(value);
            }
        }
    }

}

Cotroller入参对象加上开启校验注解 @Valid

public ResultVO<?> login(@Valid UserForm userForm) {
        System.err.println(userForm.getMobile()+":"+userForm.getPassword());
        /**密码效验*/
        SeckillUser seckillUser = seckillUserService.findByMobile(userForm.getMobile());
        System.err.println(seckillUser);
        String calcPass = MD5Util.formPassToDBPass(userForm.getPassword(), seckillUser.getSalt());
        System.err.println("calcPass:"+calcPass);
        if(calcPass.equals(seckillUser.getPassword())) {
            return  ResultVOUtil.success(seckillUser);
        }
        return ResultVOUtil.fail("登录失败");
    }

对象属性具体校验自定义注解@IsMobile

package com.bioodas.seckill.web.form;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;

import com.bioodas.seckill.annotation.IsMobile;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class UserForm {
    
    @NotNull
    @IsMobile
    private String mobile;
    @NotNull
    @Length(min=6)
    private String password;

}

上一篇下一篇

猜你喜欢

热点阅读