Reputation: 737
My project was running fine until I've added 'hibernate-validator' in my POM for using @Valid feature at REST request for @RequestBody objects.
I have a NoClassDefFoundError
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring/app-context.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
Caused by: java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
... 21 more
Caused by: java.lang.ClassNotFoundException: javax.validation.ValidatorFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 35 more
My POM is
The last line is the one which makes problems, I've tried with several versions without success
Upvotes: 1
Views: 324
Reputation: 737
None of the above answers solved my problem, so I've decided to develop my own validator
A new annotation interface
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD) //can use in fields only.
public @interface Required {
}
And a base class to extend all objects I need to validate (at spring REST @RequestBody)
public void validate(){
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Required.class)) {
Object value;
field.setAccessible(true);
try {
boolean fail = false;
value = field.get(this);
if (value==null){
fail = true;
}
if (value instanceof String && ((String) value).isEmpty()){
fail = true;
}
if (value instanceof List && ((List<?>) value).isEmpty()){
fail = true;
}
if (fail){
throw new PreconditionFailedException("attribute '" + field.getName() + "' cannot be empty");
}
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
Upvotes: 0
Reputation: 11017
add
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
Upvotes: 1
Reputation: 13835
Go to the .m2 repository and delete the folder under org/hibernate/ path. Now clean the project and again build the project. This may work fine.
Upvotes: 0