spring mvc 验证框架要么不起作用,要么报错 error creating bean 'org.springframework.validation.beanvalidation

在一台电脑运行好好的,但是到另一台电脑上运行,一直报错Error creating bean 'org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean#0'

搜半天资料,可能是版本导致的问题。

把问题机器的spring版本从4.1改为3.1,运行正常。问题机使用的myeclipse 2016+spring4.1,正常机器是eclipse10+spring3.1

另外,项目引用了validation、hibernate-validator的包,也可能是spring的版本与这几个包的版本不匹配造成。

spring mvc 验证框架要么不起作用,要么报错 error creating bean 'org.springframework.validation.beanvalidation_第1张图片


      
        username:

        userpass:

        nickname:

        email:

        
    

 

 

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

public class User {
    @NotEmpty(message="用户名不能为空")
    private String username;
    
    @Length(min=6,message="密码不低于6位")
    private String userpass;
    
    private String nickname;
    
    @Email(message="邮箱格式不正确")
    private String email;

 

    @RequestMapping(value="/add",method=RequestMethod.POST)
    public String add(@Validated User user,BindingResult br,Model model){
        //System.out.println("xxxxxxxxxxxxxxxxxx........................"+user.getUsername() +"," +user.getNickname());
        log.info("user add:"+user.getUsername() +"," +user.getNickname());
        if(br.hasErrors()){
            return "user/add";
        }
        users.put(user.getUsername(),user);
        model.addAttribute("user",user);
        return "redirect:list";  //演示1:客户端跳转,add 与list action同级。
    }
    

spring mvc 验证框架要么不起作用,要么报错 error creating bean 'org.springframework.validation.beanvalidation_第2张图片

 

 

 

附:另一个解决方案,没用maven,所以没试。

Error creating bean 'org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean#0'

2017年04月01日 10:50:45 淡然游江湖 阅读数:7755

SPRING版本:4.3.4.RELEASE

SpringMvc版本:4.3.4.RELEASE

Dubbo版本:2.8.4

增加mvc注解直接报错

 

 
  1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean#0': Invocation of init method failed; nested exception is java.lang.AbstractMethodError: org.apache.bval.jsr303.ConfigurationImpl.getDefaultParameterNameProvider()Ljavax/validation/ParameterNameProvider;

  2. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)

  3. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)

  4. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)

  5. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)

  6. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)

  7. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)

  8. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)

  9. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:754)

  10. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)

  11. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)

  12. at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)

  13. at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)

  14. at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)

  15. at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)

  16. at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)

  17. at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)

  18. at javax.servlet.GenericServlet.init(GenericServlet.java:160)

  19. at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)

  20. at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)

  21. at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)

  22. at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)

  23. at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)

  24. at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

  25. at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)

  26. at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)

  27. at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)

  28. at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1551)

  29. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  30. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

  31. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  32. at java.lang.reflect.Method.invoke(Method.java:606)

  33. at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)

  34. at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)

  35. at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)

  36. at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:622)

  37. at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:569)

  38. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  39. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

  40. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  41. at java.lang.reflect.Method.invoke(Method.java:606)

  42. at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)

  43. at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)

  44. at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)

  45. at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487)

  46. at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97)

  47. at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328)

  48. at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420)

  49. at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)

  50. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  51. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

  52. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  53. at java.lang.reflect.Method.invoke(Method.java:606)

  54. at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)

  55. at sun.rmi.transport.Transport$1.run(Transport.java:177)

  56. at sun.rmi.transport.Transport$1.run(Transport.java:174)

  57. at java.security.AccessController.doPrivileged(Native Method)

  58. at sun.rmi.transport.Transport.serviceCall(Transport.java:173)

  59. at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)

  60. at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)

  61. at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)

  62. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

  63. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

  64. at java.lang.Thread.run(Thread.java:745)

  65. Caused by: java.lang.AbstractMethodError: org.apache.bval.jsr303.ConfigurationImpl.getDefaultParameterNameProvider()Ljavax/validation/ParameterNameProvider;

  66. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  67. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

  68. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  69. at java.lang.reflect.Method.invoke(Method.java:606)

  70. at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:216)

  71. at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:201)

  72. at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.configureParameterNameProviderIfPossible(LocalValidatorFactoryBean.java:313)

  73. at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:282)

  74. at org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean.afterPropertiesSet(OptionalValidatorFactoryBean.java:40)

  75. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)

  76. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)

  77. ... 62 more

  78.  
  79.  
  80. 四月 01, 2017 10:29:28 上午 org.apache.catalina.core.StandardContext loadOnStartup

  81. 严重: Servlet /autobot-core threw load() exception

  82. java.lang.AbstractMethodError: org.apache.bval.jsr303.ConfigurationImpl.getDefaultParameterNameProvider()Ljavax/validation/ParameterNameProvider;

  83. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  84. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

  85. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  86. at java.lang.reflect.Method.invoke(Method.java:606)

  87. at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:216)

  88. at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:201)

  89. at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.configureParameterNameProviderIfPossible(LocalValidatorFactoryBean.java:313)

  90. at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:282)

  91. at org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean.afterPropertiesSet(OptionalValidatorFactoryBean.java:40)

  92. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)

  93. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)

  94. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)

  95. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)

  96. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)

  97. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)

  98. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)

  99. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)

  100. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:754)

  101. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)

  102. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)

  103. at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)

  104. at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)

  105. at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)

  106. at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)

  107. at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)

  108. at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)

  109. at javax.servlet.GenericServlet.init(GenericServlet.java:160)

  110. at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)

  111. at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)

  112. at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)

  113. at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)

  114. at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)

  115. at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

  116. at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)

  117. at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)

  118. at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)

  119. at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1551)

  120. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  121. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

  122. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  123. at java.lang.reflect.Method.invoke(Method.java:606)

  124. at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)

  125. at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)

  126. at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)

  127. at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:622)

  128. at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:569)

  129. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  130. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

  131. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  132. at java.lang.reflect.Method.invoke(Method.java:606)

  133. at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)

  134. at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)

  135. at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)

  136. at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487)

  137. at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97)

  138. at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328)

  139. at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420)

  140. at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)

  141. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  142. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

  143. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  144. at java.lang.reflect.Method.invoke(Method.java:606)

  145. at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)

  146. at sun.rmi.transport.Transport$1.run(Transport.java:177)

  147. at sun.rmi.transport.Transport$1.run(Transport.java:174)

  148. at java.security.AccessController.doPrivileged(Native Method)

  149. at sun.rmi.transport.Transport.serviceCall(Transport.java:173)

  150. at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)

  151. at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)

  152. at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)

  153. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

  154. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

  155. at java.lang.Thread.run(Thread.java:745)

 

 

解决方案

增加javax.validation-validation-api引用

 

 
  1. javax.validation

  2. validation-api

  3. 2.0.0.Alpha1

 

 

分析可能的原因

spring4之后集成了JSR-349标准,validation-api jar包需要升级

你可能感兴趣的:(java,springmvc)