我在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验证器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,因为两者都有不同的包结构。
问题内容: 我在Spring中使用javax-validation和hibaernate-validator进行表单验证。除了用于Spring的基本必要JAR文件。 我包括: 验证API-1.1.0.Final hibernate-validator-5.1.0.Final 我读到某处也关注到了spl4j版本:所以我也告诉: 错误原因可以归因于slf4j和log4j兼容性吗? 我使用注释基础验证。
如能提供任何帮助,我将不胜感激。 谢谢!
我使用Spring+Hibernate+MySQL for web应用程序进行开发。但是在过去的一周里,我总是在尝试运行orgdao.findAll()获取所有组织时遇到“nested exception is org.springframework.dao.invalidDataAccessResourceUsageException:”异常。即使我评论所有的关系属性,但仍然没有幸运。以下是代码
我正在获取dynamo db中的所有记录,对于每条记录,我都要对一个外部服务进行多线程调用。 但是我得到了这个错误: 嵌套异常是java.util.concurrent.executionException:org.springframework.web.client.HttpServererRoreXception:500 null] 编辑: 错误的完整堆栈跟踪: org.springframe
我正在应用程序中使用Windows Azure SQL Server,当我连接到应用程序时,它显示以下错误。 hibernate.cfg.xml: .................. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/cont