椭圆自定义方法验证 - 前置条件

时间:2014-11-22 15:03:11

标签: java spring validation annotations oval

我正在使用Oval验证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 {

让我知道我在这里失踪了什么。感谢。

1 个答案:

答案 0 :(得分:0)

添加约束注释的方式将其声明为返回值的约束。

对于参数验证,您需要直接在要验证的方法参数中指定注释,即:

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