用好spring mvc validator可以簡化代碼,mvcvalidator

來源:互聯網
上載者:User

用好spring mvc validator可以簡化代碼,mvcvalidator

表單的資料檢驗對一個程式來講非常重要,因為對於用戶端的資料不能完全信任,常規的檢驗類型有:

  • 參數為空白,根據不同的業務規定要求表單項是必填項
  • 參數值的有效性,比如產品的價格,一定不能是負數
  • 多個表單項組合檢驗,比如在註冊時密碼與確認密碼必須相同
  • 參數值的資料範圍,常見的是一些狀態值,或者叫枚舉值,如果傳遞的參數超出已經定義的枚舉那麼也是無意義的

上面的這些檢驗基本上都是純資料方面的,還不算具體的業務資料檢驗,下面是一些強業務相關的資料檢驗

  • 根據產品ID,去檢驗ID是否真實存在
  • 註冊使用者時,需要檢驗使用者名稱的唯一性
  • ....


根據上面的需求,如果我們將這些檢驗的邏輯全部與商務邏輯耦合在一起,那麼我們的程式邏輯將會變得冗長而且不便於代碼複用,下面的代碼就是耦合性強的一種體現:

           if (isvUserRequestDTO == null) {                log.error("can not find isv request by request id, " + isvRequestId);                return return_value_error(ErrorDef.FailFindIsv);            }            if (isvUserRequestDTO.getAuditStatus() != 1) {                log.error("isv request is not audited, " + isvRequestId);                return return_value_error(ErrorDef.IsvRequestNotAudited);            }

 

我們可以利用spring提供的validator來解耦表單資料的檢驗邏輯,可以將上述的代碼從具體的業務代碼的抽離出去。

 


Hibernate validator,它是JSR-303的一種具體實現。它是基於註解形式的,我們看一下它原生支援的一些註解。

註解 說明
@Null 只能為空白,這個用途情境比較少
@NotNull 不可為空,常用註解
@AssertFalse 必須為false,類似於常量
@AssertTrue 必須為true,類似於常量
@DecimalMax(value)  
@DecimalMin(value)  
@Digits(integer,fraction)  
@Future 代表是一個將來的時間
@Max(value) 最大值,用於一個枚舉值的資料範圍控制
@Min(value) 最小值,用於一個枚舉值的資料範圍控制
@Past 代表是一個到期的時間
@Pattern(value) Regex,比如驗證手機號,郵箱等,非常常用
@Size(max,min)

限制字元長度必須在min到max之間

 

 

 

 

 

 

 

 

 

 

 

 

 

 基礎資料類型的使用樣本

@NotNull(message = "基礎數量不可為空")    @Min(value = 0,message = "基礎數量不合法")    private Integer baseQty;


嵌套檢驗,如果一個對象中包含子物件(非基礎資料類型)需要在屬性上增加@Valid註解。

   @Valid   @NotNull(message = "價格策略內容不可為空")    private List<ProductPricePolicyItem> policyItems;


除了原生提供的註解外,我們還可以自訂一些限制性的檢驗類型,比如上面提到的多個屬性之間的聯合檢驗。該註解需要使用@Constraint標註,這裡我編寫了一個用於針對兩個屬性之間的資料檢驗的規則,它支援兩個屬性之間的如下操作符,而且可以設定多組屬性對。

  • ==
  • >
  • >=
  • <
  • <=

建立註解

  • 通過@Constraint指定檢驗的實作類別CrossFieldMatchValidator
  • 增加兩個屬性名稱欄位,用於後續的檢驗
  • 增加一個註解的List,用來支援一個對象中檢驗多組屬性對。比如即需要檢驗最大數量與最小數量,也需要檢驗密碼與確認密碼
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})@Retention(RUNTIME)@Constraint(validatedBy = CrossFieldMatchValidator.class)@Documentedpublic @interface CrossFieldMatch {    String message() default "{constraints.crossfieldmatch}";    Class<?>[] groups() default {};    Class<? extends Payload>[] payload() default {};    /**     * @return The first field     */    String first();    /**     * @return The second field     */    String second();    /**     * first operator second     * @return     */    CrossFieldOperator operator();    /**     * Defines several <code>@FieldMatch</code> annotations on the same element     *     * @see CrossFieldMatch     */    @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})    @Retention(RUNTIME)    @Documented    @interface List {        CrossFieldMatch[] value();    }}

檢驗實作類別

isValid方法,通過反射可以取到需要檢驗的兩個欄位的值以及資料類型,然後根據指定的資料操作符以及資料類型做出計算。目前這個檢驗只針對我的業務並不十分通用,需要根據自己的項目情況來靈活處理。

public class CrossFieldMatchValidator implements ConstraintValidator<CrossFieldMatch, Object> {    private String firstFieldName;    private String secondFieldName;    private CrossFieldOperator operator;    @Override    public void initialize(final CrossFieldMatch constraintAnnotation) {        firstFieldName = constraintAnnotation.first();        secondFieldName = constraintAnnotation.second();        operator=constraintAnnotation.operator();    }    @Override    public boolean isValid(final Object value, final ConstraintValidatorContext context) {        try {            Class valueClass=value.getClass();            final Field firstField = valueClass.getDeclaredField(firstFieldName);            final Field secondField = valueClass.getDeclaredField(secondFieldName);            //不支援為null的欄位            if(null==firstField||null==secondField){                return false;            }            firstField.setAccessible(true);            secondField.setAccessible(true);            Object firstFieldValue= firstField.get(value);            Object secondFieldValue= secondField.get(value);            //不支援類型不同的欄位            if(!firstFieldValue.getClass().equals(secondFieldValue.getClass())){                return false;            }            //整數支援 long int short            //浮點數支援 double            if(operator==CrossFieldOperator.EQ) {                return firstFieldValue.equals(secondFieldValue);            }            else if(operator==CrossFieldOperator.GT){                if(firstFieldValue.getClass().equals(Long.class)||firstFieldValue.getClass().equals(Integer.class)||firstFieldValue.getClass().equals(Short.class)) {                    return (Long)firstFieldValue > (Long) secondFieldValue;                }                else if(firstFieldValue.getClass().equals(Double.class)) {                    return (Double)firstFieldValue > (Double) secondFieldValue;                }            }            else if(operator==CrossFieldOperator.GE){                if(firstFieldValue.getClass().equals(Long.class)||firstFieldValue.getClass().equals(Integer.class)||firstFieldValue.getClass().equals(Short.class)) {                    return Long.valueOf(firstFieldValue.toString()) >= Long.valueOf(secondFieldValue.toString());                }                else if(firstFieldValue.getClass().equals(Double.class)) {                    return Double.valueOf(firstFieldValue.toString()) >= Double.valueOf(secondFieldValue.toString());                }            }            else if(operator==CrossFieldOperator.LT){                if(firstFieldValue.getClass().equals(Long.class)||firstFieldValue.getClass().equals(Integer.class)||firstFieldValue.getClass().equals(Short.class)) {                    return (Long)firstFieldValue < (Long) secondFieldValue;                }                else if(firstFieldValue.getClass().equals(Double.class)) {                    return (Double)firstFieldValue < (Double) secondFieldValue;                }            }            else if(operator==CrossFieldOperator.LE){                if(firstFieldValue.getClass().equals(Long.class)||firstFieldValue.getClass().equals(Integer.class)||firstFieldValue.getClass().equals(Short.class)) {                    return Long.valueOf(firstFieldValue.toString()) <= Long.valueOf(secondFieldValue.toString());                }                else if(firstFieldValue.getClass().equals(Double.class)) {                    return Double.valueOf(firstFieldValue.toString()) <= Double.valueOf(secondFieldValue.toString());                }            }        }        catch (final Exception ignore) {            // ignore        }        return false;    }}

 

調用樣本:

@CrossFieldMatch.List({        @CrossFieldMatch(first = "minQty", second = "maxQty",operator = CrossFieldOperator.LE ,message = "最小數量必須小於等於最大數量")})public class ProductPriceQtyRange implements Serializable{    /**     * 最小數量     */    @Min(value = 0,message = "最小數量不合法")    private int minQty;    /**     * 最大數量     */    @Min(value = 0,message = "最大數量不合法")    private int maxQty;    public int getMinQty() {        return minQty;    }    public void setMinQty(int minQty) {        this.minQty = minQty;    }    public int getMaxQty() {        return maxQty;    }    public void setMaxQty(int maxQty) {        this.maxQty = maxQty;    }}

 


需要在mvc的設定檔中增加如下節點以啟動檢驗

 <mvc:annotation-driven validator="validator">  <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>        <property name="validationMessageSource" ref="messageSource"/>    </bean>    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">        <property name="useCodeAsDefaultMessage" value="false"/>          <property name="defaultEncoding" value="UTF-8"/>    </bean>

經過上面在對象屬性上的資料檢驗註解,我們將大部分的資料檢驗邏輯從商務邏輯中轉移出去,不光是精簡了代碼還使得原本複雜的代碼變得簡單清晰,代碼的重複利用率也增強了。

 

本文引用:

http://stackoverflow.com/questions/1972933/cross-field-validation-with-hibernate-validator-jsr-303

 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.