2014-11-22 52 views
0

我正在使用橢圓驗證Fwk + ​​Spring。我想驗證我的業務邏輯方法使用註釋。我在橢圓形頁面中找到了一個很好的例子。 http://best-practice-software-engineering.ifs.tuwien.ac.at/repository/net/sf/oval/oval/1.61/tmp/docs/userguide.html#d4e489橢圓定製方法驗證 - 前提條件

用於創建自定義驗證。但提供的示例似乎在該方法執行之後而不是之前工作。有沒有辦法達到與例子相同的效果,但之前執行過嗎?

這裏是我的彈簧配置。

<bean id="ovalGuardInterceptor" class="net.sf.oval.guard.GuardInterceptor" /> 

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
    <property name="proxyTargetClass" value="true" /> 
    <property name="beanNames" value="productGetBusinessLogic" /> 
    <property name="interceptorNames"><list><value>ovalGuardInterceptor</value></list></property> 
</bean> 

這是我的支票類

import net.sf.oval.Validator; 
import net.sf.oval.configuration.annotation.AbstractAnnotationCheck; 
import net.sf.oval.context.OValContext; 

public class TestValidatorCheck extends AbstractAnnotationCheck<TestValidator> { 
    public boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context, Validator validator) { 
    if (valueToValidate == null) 
     return true; 
    String val = valueToValidate.toString(); 
    return val.equals(val.toUpperCase()); 
    } 
} 

這是我的註釋類

@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.METHOD}) 
@Documented 
@net.sf.oval.configuration.annotation.Constraint(checkWith = TestValidatorCheck.class) 
public @interface TestValidator { 
/** 
* Message to be used for the ConstraintsViolatedException 
* 
* @see ConstraintsViolatedException 
*/ 
String message() default "must be upper case"; 
} 

,這是怎樣的方法都添加

@Override 
@TestValidator 
public ProductGetResponse getProductBulk(ProductGetKey productGetKey) throws ItemWrapperApiException { 

讓我知道我在這裏失蹤。謝謝。

回答

0

您添加約束註釋的方式將其聲明爲返回值的約束。

對於您需要直接在方法參數中指定的註釋進行驗證參數驗證,即:

@Override 
public ProductGetResponse getProductBulk(@TestValidator ProductGetKey productGetKey) throws ItemWrapperApiException