当前位置: 首页 > 知识库问答 >
问题:

Hibernate-Validator中出现异常。嵌套异常为java.lang.NoClassDefoundError:ConfigurationImpl

咸高谊
2023-03-14

我在Spring中使用javax-validation和hibaernate-validator进行表单验证。而不是Spring所必需的基本JAR文件。

我在某处读到spl4j版本也受到关注,所以我也告诉大家:

###slf4j-api-1.7.5###
###slf4j-simple-1.7.5###
###log4j-1.2.17###

错误的原因会不会是由于slf4j和log4j的兼容性?

我使用基于注释的验证。

import java.util.Date;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;


public class Customer {

    @Size(min=2, max=30)
    @NotEmpty
    private String name;

    @NotEmpty @Email
    private String email;

    @NotNull @Min(18) @Max(100)
    private Integer age;
public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    @Qualifier("messageSource")
    private MessageSource messageSource;

    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody List<String> saveCustomerAction(
             @Valid @ModelAttribute("customer") Customer customer,
            BindingResult bindingResult, Model model) {
         List<String> errorLog = new ArrayList<String>();
        if (bindingResult.hasErrors()) {
     errorLog.add("Error");

                }
            }
            return errorLog;
        }

        errorLog.add("No Error");
        return errorLog;
    }
}
type Exception report

   message Servlet.init() for servlet spring-rest threw exception

   description The server encountered an internal error that prevented it from fulfilling this request.

   exception

   javax.servlet.ServletException: Servlet.init() for servlet spring-rest threw exception
       org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
       org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
       org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
       org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
       org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
       org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
       org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
       java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
       java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
       org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
       java.lang.Thread.run(Thread.java:745)

###----###
###root cause###

    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.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.ConfigurationImpl
        org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
        org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
        org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
        org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
        org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
        org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
        org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
        org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
        org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
        org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
        org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
        org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
        org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
        org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
        org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
        org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
        javax.servlet.GenericServlet.init(GenericServlet.java:158)
        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
        org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
        org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
        java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.lang.Thread.run(Thread.java:745)

共有1个答案

濮阳原
2023-03-14

请参阅Hibernate Validator常见问题解答

http://hibernate.org/validator/faq/

Hibernate验证器3、4和5有什么区别?

Hibernate验证器3.x和4.x/5.x是不同的代码库。

Hibernate Validator是来自Hibernate团队的原始验证框架,现在称为“遗留Hibernate Validator”。Hibernate Validator4.x是Bean Validation 1.0(JSR303)的参考实现,而Hibernate Validator5.x是Bean Validation 1.1(JSR349)的参考实现。活动开发发生在5.x代码库上。

在使用spring validation framework for MVC时:

 <!-- JSR-303 support will be detected on classpath and enabled automatically -->
 <mvc:annotation-driven />

因此,差异可能是:hibernate-validator-5.x不支持JSR303,尝试将版本降级到4.x。

也更喜欢Hibernate Validator 4.3.1.final。而不是4.2.x,因为两者都有不同的包结构。

 类似资料: