我使用的是Oval validation Fwk + Spring。我想使用注释来验证我的业务逻辑方法。我在Oval Page中找到了一个很好的例子。http://best-practice-software-engineering.ifs.tuwien.ac.at/repository/net/sf/oval/oval/1.61/tmp/docs/userguide.html#d4e489
用于创建自定义验证。但是,所提供的示例似乎是在方法执行之后而不是之前工作的。有没有一种方法可以实现与之前执行的示例相同的功能?
这是我的spring配置。
<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>这是我的check类
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 {让我知道我在这里错过了什么。谢谢。
发布于 2015-07-15 00:51:55
添加约束注释的方式将其声明为返回值的约束。
对于参数验证,您需要直接在要验证的方法参数中指定注释,即:
@Override
public ProductGetResponse getProductBulk(@TestValidator ProductGetKey productGetKey) throws ItemWrapperApiExceptionhttps://stackoverflow.com/questions/27078799
复制相似问题