gpt4 book ai didi

java - hibernate 验证程序中的异常。嵌套异常是 java.lang.NoClassDefFoundError : ConfigurationImpl

转载 作者:搜寻专家 更新时间:2023-11-01 02:25:23 25 4
gpt4 key购买 nike

详情:

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

我已经包括:

validation-api-1.1.0.Final

hibernate-validator-5.1.0.Final

我在某处读到 spl4j 版本也有问题:所以我也告诉:

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

会不会是slf4j和log4j不兼容导致报错?

我使用注释基础验证。

让我提供一些代码:

客户.java

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;
}

}

客户 Controller .java

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;
}
}

为消息源和消息添加了必需的 Bean 和 message_en.propertires

错误日志

-----

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)

最佳答案

这是根据 Hibernate Validator 常见问题解答

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

Hibernate Validator 3、4 和 5 之间有什么区别?

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

Hibernate Validator 是 Hibernate 团队的原始验证框架,现在称为“Legacy Hibernate Validator”。 Hibernate Validator 4.x 是 Bean Validation 1.0 (JSR 303) 的引用实现,而 Hibernate Validator 5.x 是 Bean Validation 1.1 (JSR 349) 的引用实现。在 5.x 代码库上进行积极的开发。

此外,当您使用 MVC 的 spring 验证框架时:

 <!-- 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,因为两者具有不同的包结构。

关于java - hibernate 验证程序中的异常。嵌套异常是 java.lang.NoClassDefFoundError : ConfigurationImpl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24957513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com