1. 程式人生 > >Java使用Validator進行Bean校驗

Java使用Validator進行Bean校驗

  • 工具類
package com.yjy.util;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yjy.exception.ParamException;
import org.apache.commons.collections.MapUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import java.util.*; public class BeanValidator { private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); public static <T> Map<String, String> validate(T t, Class... groups) { Validator validator = validatorFactory.getValidator
(); Set validateResult = validator.validate(t, groups); if (validateResult.isEmpty()) { return Collections.emptyMap(); } else { LinkedHashMap errors = Maps.newLinkedHashMap(); Iterator iterator = validateResult.iterator(); while (iterator.hasNext
()) { ConstraintViolation violation = (ConstraintViolation) iterator.next(); errors.put(violation.getPropertyPath().toString(), violation.getMessage()); } return errors; } } public static Map<String, String> validateList(Collection<?> collection) { Preconditions.checkNotNull(collection); Iterator iterator = collection.iterator(); Map errors; do { if (!iterator.hasNext()) { return Collections.emptyMap(); } Object object = iterator.next(); errors = validate(object, new Class[0]); } while (errors.isEmpty()); return errors; } public static Map<String, String> validateObject(Object first, Object... objects) { if (objects != null && objects.length > 0) { return validateList(Lists.asList(first, objects)); } else { return validate(first, new Class[0]); } } public static void check(Object param) throws ParamException { Map<String, String> map = BeanValidator.validateObject(param); if (MapUtils.isNotEmpty(map)) { throw new ParamException(map.toString()); } } }
  • 示例Bean
package com.yjy.param;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@Getter
@Setter
public class UserParam {

    private Integer id;

    @NotBlank(message = "使用者名稱不可以為空")
    @Length(min = 1, max = 20, message = "使用者名稱長度需要在20個字以內")
    private String username;

    @NotBlank(message = "電話不可以為空")
    @Length(min = 1, max = 13, message = "電話長度需要在13個字以內")
    private String telephone;

    @NotBlank(message = "郵箱不允許為空")
    @Length(min = 5, max = 50, message = "郵箱長度需要在50個字元以內")
    private String mail;

    @NotNull(message = "必須提供使用者所在的部門")
    private Integer deptId;

    @NotNull(message = "必須指定使用者的狀態")
    @Min(value = 0, message = "使用者狀態不合法")
    @Max(value = 2, message = "使用者狀態不合法")
    private Integer status;

    @Length(min = 0, max = 200, message = "備註長度需要在200個字以內")
    private String remark = "";
}