1. 程式人生 > 程式設計 >基於javax.validation結合spring的最佳實踐

基於javax.validation結合spring的最佳實踐

前言

本人先將用到的配置、工具類貼出來,然後一步步告訴大家怎麼使用

R303 是一套Bean引數校驗的標準,它定義了很多常用的校驗註解,我們可以直接將這些註解加在我們JavaBean的屬性上面,就可以在需要校驗的時候進行校驗了。

註解如下:

這裡寫圖片描述

Hibernate validator 在JSR303的基礎上對校驗註解進行了擴充套件,擴充套件註解如下:

這裡寫圖片描述

pom中添加註解

        <!--jsr 303-->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <!-- hibernate validator-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.0.Final</version>
        </dependency>

Spring配置

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

自己的Violation實體

這裡使用的Lombok獲取get和set,使用的@Getter註解

@AllArgsConstructor 這個註解是lombok中為類提供一個全參的構造方法

package com.alibaba.xianzhi.validator;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * Created by Jackielee on 2017
 * @author: lizhilong
 * @date:   2017-11-14 18:01:34   
 */
@Getter
@AllArgsConstructor
public class Violation implements Serializable {
    private static final long serialVersionUID = -1731546219600067986L;
    private final String message;
    private final Object bean;
    private final String property;
    private final Object value;
}

封裝一個ViolationBuild

後面會說這個類的用法

package com.alibaba.xianzhi.validator;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintViolation;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.collections.CollectionUtils;

/**
 * @author: lizhilong
 * @date:   2017-11-15 11:41:12   
 */
@AllArgsConstructor
public class ViolationBuild{

    @Getter
    private Set<Violation> violations;

    public String getMessage() {
        List<String> list = new ArrayList<String>();
        for (Violation violation : violations) {
            list.add(violation.getMessage());
        }
        return list.size() > 0 ? list.get(0) : "";
    }

    public static <T> ViolationBuild build(Set<ConstraintViolation<T>> cvs) {
        Set<Violation> result = new HashSet<Violation>();
        if (CollectionUtils.isNotEmpty(cvs)) {
            for (ConstraintViolation cv : cvs) {
                result.add(new Violation(cv.getMessage(),cv.getRootBean() == null ? null : cv.getRootBean().toString(),cv.getPropertyPath() == null ? null : cv.getPropertyPath().toString(),cv.getInvalidValue()));
            }
        }
        return new ViolationBuild(result);
    }
}

用來校驗實體,構建並存儲校驗後的資訊ValidatorProvider

package com.alibaba.xianzhi.validator;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ValidationException;
import javax.validation.Validator;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
 * @author: lizhilong
 * @date:   2017-11-15 11:40:59   
 */
@AllArgsConstructor
@Getter
public class ValidatorProvider {
    private final Validator validator;

    public <T> ViolationBuild validate(T object) {
        Set<ConstraintViolation<T>> violations;
        tr客棧y {
            violations = validator.validate(object);
        } catch (IllegalArgumentException iae) {
            throw iae;
        } catch (ValidationException ve) {
            throw ve;
        }
        return ViolationBuild.build(violations);
    }

    public <T> ViolationBuild validate(T object,Class<?>... groups) {
        Set<ConstraintViolation<T>> violations;
        try {
            violations = validator.validate(object,groups);
        } catch (IllegalArgumentException iae) {
            throw iae;
        } catch (ValidationException ve) {
            throw ve;
        }
        return ViolationBuild.build(violations);
    }

    public <T> ViolationBuild validateProperty(T object,String propertyName,Class<?>... groups) {
        Set<ConstraintViolation<T>> violations;
        try {
            violations = validator.validateProperty(object,propertyName,groups);
        } catch (IllegalArgumentException iae) {
            throw iae;
        } catch (ValidationException ve) {
            throw ve;
        }
        return ViolationBuild.build(violations);
    }

    public <T> ViolationBuild validateValue(Class<T> beanType,Object value,Class<?>... groups) {
        Set<Conshttp://www.cppcns.comtraintViolation<T>> violations;
        try {
            violations = validator.validateValue(beanType,value,groups);
        } catch (IllegalArgumentException iae) {
            throw iae;
        } catch (ValidationException ve) {
            throw ve;
        }
        return ViolationBuild.build(violations);
    }
}

BaseService讓自己的service繼承此類

java不能多繼承 所以如果已經繼承了別的類,可以將此類注入出來

此類是為了拿到一個單例的ValidatorProvider

package com.alibaba.xianzhi.base.web;
import javax.annotation.Resource;
import javax.validation.Validator;
import com.alibaba.xianzhi.validator.ValidatorProvider;

/**
 * BaseService
 * @author: lizhilong
 * @date:   2017-11-15 11:41:24   
 */
public abstract class BaseService {

    @Resource
    protected Validator validator;

    private ValidatorProvider validatorProvider;

    protected ValidatorProvider getValidatorProvider() {
        if (validatorProvider == null) {
            validatorProvider = new ValidatorProvider(validator);
        }
        return validatorProvider;
    }
}

所需校驗的實體類

說明:Constants為介面常量

@Getter
@AllArgsConstructor
public class SubmitVO extends BaseVO {

    @NotNull(message="廠商不能為空")
    private Long companyId;

    @Length(min=0,max=100,message="標題請控制在" + Constants.MAX_TITLE + "個字元以內")
    @NotNull(message="標題不能為空")
    private String title;

    @Length(min=0,max=65535,message="修復方案長度不能超過" + Constants.MAX_FIX_ADVICE)
    @NotNull(message="修復方案不能為空")
    private String fixAdvice;
}

下面就是如何使用

public BaseResponse save(SubmitVO submitVO ) {
/**
 * getValidatorProvider()此方法是BaserService中,
 * 上面說到本人的service是繼承此  
 * service的所以可以直接用
 **/
ValidatorProvider validatorProvider = getValidatorProvider();
/**
 * validatorProvider呼叫validate(Object obj)進行校驗
 * 返回ViolationBuild 
 **/
ViolationBuild validateFlaw = validatorProvider.validate(submitVO);
/**
 * 此時如果校驗有失敗的 容器中便會儲存資訊,ViolationBuild可以通過自己的
 * getMessage方法獲取資訊(此方法可以自己封裝成自己想要的樣子)
 **/
System.out.println(validateFlaw.getMessage());
 }

列印結果:標題請控制在100個字元以內

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援我們。