0
我建立使用Spring一個簡單的應用程序,我努力實現自定義JSR303
標註驗證方法字符串參數。我用Java代碼來配置Spring容器,我認爲我已經加載,我需要的所有的Spring bean。但驗證註釋仍然不起作用。問題與使用自定義JSR303註解來驗證方法的參數在Spring MVC容器
下面是代碼:
配置Spring:
@Configuration
@Lazy(false)
public class MVCContainerConfig
{
@Bean
public AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter()
{
ConfigurableWebBindingInitializer configurableWebBindingInitializer = new ConfigurableWebBindingInitializer();
configurableWebBindingInitializer.setValidator(localValidatorFactoryBean());
AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter = new AnnotationMethodHandlerAdapter();
annotationMethodHandlerAdapter.setWebBindingInitializer(configurableWebBindingInitializer);
annotationMethodHandlerAdapter.setMessageConverters(new HttpMessageConverter[]{
new BufferedImageHttpMessageConverter(),
new ByteArrayHttpMessageConverter(),
new FormHttpMessageConverter(),
new ResourceHttpMessageConverter(),
new StringHttpMessageConverter(),
new AtomFeedHttpMessageConverter(),
new RssChannelHttpMessageConverter(),
new MappingJacksonHttpMessageConverter(),
new Jaxb2RootElementHttpMessageConverter(),
new MarshallingHttpMessageConverter(),
new XmlAwareFormHttpMessageConverter()
});
return annotationMethodHandlerAdapter;
}
@Bean
public DefaultAnnotationHandlerMapping defaultAnnotationHandlerMapping()
{
DefaultAnnotationHandlerMapping defaultAnnotationHandlerMapping = new DefaultAnnotationHandlerMapping();
defaultAnnotationHandlerMapping.setInterceptors(new Object[]{localeChangeInterceptor(),
themeChangeInterceptor()});
return defaultAnnotationHandlerMapping;
}
@Bean(name="validator")
public LocalValidatorFactoryBean localValidatorFactoryBean()
{
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
return localValidatorFactoryBean;
}
... (ignore useless code)
}
註釋定義:
@Documented
@Constraint(validatedBy={NotEmptyOrWhitespaceValidatorImp.class})
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@ReportAsSingleViolation
@NotNull
public @interface NotEmptyOrWhitespace
{
//TODO change this when language package is ready
public abstract String message() default "Empty or white space error message";
public abstract Class<?>[] groups() default { };
public abstract Class<? extends Payload>[] payload() default { };
/**
* Defines several {@code @NotEmptyOrWhitespace} annotations on the same element.
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
public @interface List {
NotEmptyOrWhitespace[] value();
}
}
約束定義:
public class NotEmptyOrWhitespaceValidatorImp implements ConstraintValidator<NotEmptyOrWhitespace, String>
{
public void initialize(NotEmptyOrWhitespace annotation){}
public boolean isValid(String str, ConstraintValidatorContext constraintValidatorContext)
{
str = str.replaceAll(" ", "");
return ((str == null || str.isEmpty()) ? false : true);
}
}
我要測試的方法:
public boolean isOurProduct(@NotEmptyOrWhitespace String productName)
{
productName = productName.trim().toLowerCase();
return this.productSet.contains(productName);
}
JUnit測試方法:
@Test
public void testIsOurProduct()
{
// If the annotation works, then I should see an exception occurred instead of the output
System.out.println("********* "+this.finder.isOurProduct(" "));
}
你`MVCContainerConfig`類甚至不會編譯。請給我們編譯示例代碼。 – skaffman 2010-11-29 09:53:14