0

In a JSF application running on Tomcat: Using @Inject in a ConstraintValidator results in a javax.validation.ValidationException: HV000028: Unexpected exception during isValid call.

Elsewhere in the application, CDI works without errors.

The ConstraintValidator:

public class MyIntervalValidator implements ConstraintValidator<MyInterval, Integer> {

    @Inject
    private Interval interval;

    public void initialize(MyInterval myIntervall) {
    }

    public boolean isValid(Integer i, ConstraintValidatorContext ctx) {

        Integer ceiling;

        ceiling = interval.getCeiling();

        if ((0 <= i) && (i <= ceiling)) {
            return true;
        }

        return false;
    }

I checked the injection: interval is null during the execution of the method isValid.

The Interval:

@SessionScoped
public class Interval implements Serializable {

    private static final long serialVersionUID = 1L;

    @Min(value = 1)
    private Integer ceiling;

    public Interval() {
        this.ceiling = 1;
    }

    public Integer getCeiling() {
        return ceiling;
    }

    public void setCeiling(Integer ceiling) {
        this.ceiling = ceiling;
    }

}

The context.xml:

<?xml version="1.0" encoding="UTF-8"?>

<Context>
    <Resource name="BeanManager" 
        auth="Container"
        type="javax.enterprise.inject.spi.BeanManager"
        factory="org.jboss.weld.resources.ManagerObjectFactory" />
</Context>

Detail from the web.xml:

<resource-env-ref>
   <resource-env-ref-name>BeanManager</resource-env-ref-name>
   <resource-env-ref-type>
      javax.enterprise.inject.spi.BeanManager
   </resource-env-ref-type>
</resource-env-ref>

Information regarding the environment:

  • Maven
  • Tomcat 8 via Eclipse Neon
  • CDI: weld-servlet-core (2.4.1.Final; empty bean.xml in folder WEB-INF)
  • Bean Validation: hibernate-validator (5.3.3.Final)
  • JSF: com.sun.faces (2.2.14)

Many thanks for any help.

jhead
  • 11
  • 2

1 Answers1

0

You should include the Hibernate Validator CDI integration module in your dependencies:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator-cdi</artifactId>
    <version>5.3.3.Final</version>
</dependency>

Maybe this is missing in your setup?

chkal
  • 5,598
  • 21
  • 26
  • Hi Christian. Many Thanks for your hint. However, I had already inserted this dependency. Unfortunately, the behavior has not changed. The same exception is thrown. Is further configuration necessary? – jhead Dec 11 '16 at 11:43