2017-08-22 90 views
1

我試圖在WAS 8.5.5.12完整配置文件中使用Spring MVC控制器進行JSR 303 bean驗證。JSR 303在WebSphere上驗證Spring(MVC)

我確認只是一個單一的@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>) 

基於

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/beanvalidation/MethodValidationPostProcessor.html

Spring應該自動檢測一個可用的JSR-303提供者,其中this version of WebSphere (8.5.5.12 full profile) should have。它似乎無法找到JSR-303提供者,所以它回落到默認值。

那麼,爲什麼沒有找到WebSphere版本或者如何找到它?

回答

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> 

最可能的是,你需要設置classloaderMode在deployment.xml中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> 

我不知道,但我包驗證的API 1.1.0戰爭,它爲我工作。

+0

但我不想使用Hibernate驗證器,我想使用WebSphere提供的驗證器。 – dbreaux