我试图在WAS 8.5.5.12完整档案中使用Spring MVC Controller进行JSR 303 bean验证。
我只验证一个@RequestParam,而不是一个完整的bean:
@RequestMapping(method=RequestMethod.GET)
public String initializeCheckIn(
@Valid @Pattern(regexp = "^[\\p{Alnum}]*$")
@RequestParam("officeid") String officeId, HttpSession session, Model model) {
在我添加一些Spring-specific configuration之前,没有进行验证,但也没有任何错误。据推测,根本没有尝试验证。
现在我已经添加了必要的@Validated类注释和bean定义:
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/>
我收到以下错误:
[8/9/17 10:33:40:588 CDT] 000000a7 ServletWrappe E com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0014E: Uncaught service() exception root cause Spring MVC Dispatcher: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org.hibernate.validator.method.MethodConstraintViolationException
...
Caused by: java.lang.NoClassDefFoundError: org.hibernate.validator.method.MethodConstraintViolationException
at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:152)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at my.package.web.CheckInController$$EnhancerBySpringCGLIB$$d385737c.initializeCheckIn(<generated>)
基于
Spring应该自动检测可用的JSR-303提供程序,this version of WebSphere (8.5.5.12 full profile) should have。它似乎未能找到JSR-303提供商,因此它又回到了默认状态。
那么为什么没有找到WebSphere版本或如何找到它的任何想法?
答案 0 :(得分:0)
首先,您必须强制使用随附的Hibernate Validator 5而不是WebLogic。只需添加 WEB-INF / validation.xml
即可<?xml version="1.0" encoding="UTF-8"?>
<validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration http://www.jboss.org/xml/ns/javax/validation/configuration/validation-configuration-1.0.xsd">
<default-provider>org.hibernate.validator.HibernateValidator</default-provider>
</validation-config>
最有可能的是,您需要在 deployment.xml 中将 classloaderMode 设置为 PARENT_LAST 。在EAR的情况下,它应该看起来像
<?xml version="1.0" encoding="UTF-8"?>
<appdeployment:Deployment xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:appdeployment="http://www.ibm.com/websphere/appserver/schemas/5.0/appdeployment.xmi" xmi:id="Deployment_1">
<deployedObject xmi:type="appdeployment:ApplicationDeployment" xmi:id="ApplicationDeployment_1" deploymentId="0" startingWeight="1">
<modules xmi:type="appdeployment:WebModuleDeployment" xmi:id="WebModuleDeployment_1" deploymentId="1" startingWeight="10000" uri="foureyes-ri.war" classloaderMode="PARENT_LAST"/>
</deployedObject>
</appdeployment:Deployment>
我不确定,但我将validation-api 1.1.0打包到WAR,它对我有用。
答案 1 :(得分:0)
我想我终于找到了一个有效的解决方案。看来我需要明确地告诉Spring MethodValidationPostProcessor
bean使用容器提供的javax.validation.ValidatorFactory
,显然我可以从JNDI lookup of "java:comp/ValidatorFactory"获得。
<jee:jndi-lookup id="validatorFactory" jndi-name="java:comp/ValidatorFactory" resource-ref="false"/>
<bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
<property name="validatorFactory" ref="validatorFactory"/>
</bean>
(顺便提一下,基于this simple JNDI lookup JSP,在此版本的WebSphere中,实现类为org.apache.bval.jsr303.ApacheValidatorFactory
)
编辑:我在博客条目中写了整个过程:https://www.ibm.com/developerworks/community/blogs/Dougclectica/entry/Spring_MVC_JSR_303_Validation_and_WebSphere