I can't integrate Hibernate Validator into a javaweb project using the spring framework

David Jones :

I tried to add hibernate validation to my web project, but if there is a jar package in the project: spring-context-indexer-5.0.6.RELEASE.jar, it will cause the program to be thrown when the program is running: java.lang. annotation.AnnotationFormatError: Duplicate annotation for class This exception. This will prevent me from integrating hibernate validation into my web project. How can I solve this problem?

 package CarTest;

import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 *
 * @author 
 */
       class Car {
    @NotNull
    private String manufacturer;

    @NotNull
    @Size(min = 2, max = 14)
    private String licensePlate;

    @Min(2)
    private int seatCount;

    public Car(String manufacturer, String licencePlate, int seatCount) {
        this.manufacturer = manufacturer;
        this.licensePlate = licencePlate;
        this.seatCount = seatCount;
    }
    //getters and setters ...
}

public class CarTest {

    public static void main(String[] args) {
        // TODO code application logic here
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Car car = new Car(null, "DD-AB-123", 4 );
    Set<ConstraintViolation<Car>> constraintViolations =     validator.validate(car);         
        System.out.println(constraintViolations.size());
        System.out.println("must not be null:-----"+constraintViolations.iterator().next().getMessage()); 
    }

}

If you add the spring-context-indexer-5.0.6.RELEASE jar to the library, the following exception will occur.

INFO: HV000001: Hibernate Validator 6.0.16.Final
Exception in thread "main" java.lang.annotation.AnnotationFormatError: Duplicate annotation for class: interface javax.validation.constraints.NotNull: @javax.validation.constraints.NotNull(message={javax.validation.constraints.NotNull.message}, groups=[], payload=[])
    at sun.reflect.annotation.TypeAnnotationParser.mapTypeAnnotations(TypeAnnotationParser.java:360)
    at sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl.<init>(AnnotatedTypeFactory.java:139)
    at sun.reflect.annotation.AnnotatedTypeFactory.buildAnnotatedType(AnnotatedTypeFactory.java:65)
    at sun.reflect.annotation.TypeAnnotationParser.buildAnnotatedType(TypeAnnotationParser.java:79)
    at java.lang.reflect.Field.getAnnotatedType(Field.java:1170)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findCascadingMetaData(AnnotationMetaDataProvider.java:618)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findPropertyMetaData(AnnotationMetaDataProvider.java:236)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getFieldMetaData(AnnotationMetaDataProvider.java:225)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.retrieveBeanConfiguration(AnnotationMetaDataProvider.java:133)
    at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfiguration(AnnotationMetaDataProvider.java:124)
    at org.hibernate.validator.internal.metadata.BeanMetaDataManager.getBeanConfigurationForHierarchy(BeanMetaDataManager.java:232)
    at org.hibernate.validator.internal.metadata.BeanMetaDataManager.createBeanMetaData(BeanMetaDataManager.java:199)
    at org.hibernate.validator.internal.metadata.BeanMetaDataManager.getBeanMetaData(BeanMetaDataManager.java:166)
    at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:157)
    at CarTest.CarTest.main(CarTest.java:47)

This is the jar package I introduced.

enter image description here

This is just a small case. Because hibernate validation is added to my web project, these jar packages are inevitably put together.But I don't know how to solve this abnormality, I hope everyone can help.

Guillaume Smet :

Are you using Eclipse or anything using the JDT compiler from Eclipse?

Because I had a very similar issue with it when working on Hibernate Validator (I'm the project lead of HV).

It doesn't happen with all the tests of the code base but for some, it just ends up with this error.

When compiling with javac (using Maven for instance), the tests pass correctly (and I can even run things from Eclipse as long as it's not the JDT compiling the classes).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=163174&siteId=1