我在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)
根据Hibernate Validator常见问题解答,网址为
http://hibernate.org/validator/faq/
Hibernate Validator 3、4和5有什么区别?
Hibernate Validator 3.x和4.x / 5.x是不同的代码库。
Hibernate Validator是Hibernate团队的原始验证框架,现在称为“传统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,因为两者都有不同的包结构。
我在Spring中使用javax-validation和hibaernate-validator进行表单验证。而不是Spring所必需的基本JAR文件。 我在某处读到spl4j版本也受到关注,所以我也告诉大家: 错误的原因会不会是由于slf4j和log4j的兼容性? 我使用基于注释的验证。
我正在获取dynamo db中的所有记录,对于每条记录,我都要对一个外部服务进行多线程调用。 但是我得到了这个错误: 嵌套异常是java.util.concurrent.executionException:org.springframework.web.client.HttpServererRoreXception:500 null] 编辑: 错误的完整堆栈跟踪: org.springframe
如能提供任何帮助,我将不胜感激。 谢谢!
问题内容: 我想捕获一个异常,该异常嵌套在另一个异常中。我目前正在这种方式: 有没有一种方法可以使此操作更高效,更优雅? 问题答案: 没有更优雅的方法来选择性地“捕获”嵌套异常。我想如果您做了很多这样的嵌套异常,就可以将代码重构为通用的实用程序方法。但是它仍然不会优雅或高效。 优雅的解决方案是消除异常嵌套。要么不首先将异常链接起来,要么(有选择地)解开包装并将嵌套的异常重新扔到堆栈的上方。 嵌套异
我使用Spring+Hibernate+MySQL for web应用程序进行开发。但是在过去的一周里,我总是在尝试运行orgdao.findAll()获取所有组织时遇到“nested exception is org.springframework.dao.invalidDataAccessResourceUsageException:”异常。即使我评论所有的关系属性,但仍然没有幸运。以下是代码