[jboss-jira] [JBoss JIRA] (WFLY-3088) Cannot Inject Validator using @Inject

Sat B (JIRA) issues at jboss.org
Fri Mar 7 15:26:33 EST 2014


Sat B created WFLY-3088:
---------------------------

             Summary: Cannot Inject Validator using @Inject
                 Key: WFLY-3088
                 URL: https://issues.jboss.org/browse/WFLY-3088
             Project: WildFly
          Issue Type: Bug
      Security Level: Public (Everyone can see)
          Components: CDI / Weld
    Affects Versions: 8.0.0.Final
            Reporter: Sat B
            Assignee: Stuart Douglas


I have a very simple bean and I am trying to inject the Validator using the @Inject annotation. But I am ending up getting a null when I try to use the Validator. I have the beans.xml defined to bean-discovery-mode="all". Below is the code

--------

import javax.inject.Inject;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.*;
import java.util.Map;
import java.util.Set;

public class SimpleCommand {
    @Inject
    private Validator validator;
   
    @Email
    private String email;
    

    public SimpleCommand(){

    }

    public SimpleCommand(Map<String, String[]> params){
        this.email = params.get("email")!=null ? params.get("email")[0] : null;
    }

    public void validate(){
        //USING VALIDATOR THAT IS INJECTED IS NULL HERE
        //USING THE BELOW METHOD WORKS
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator1 = factory.getValidator();
        Set<ConstraintViolation<SimpleCommand>> contstraintViolations = validator1.validate(this); //USING INJECTED VALIDATOR WILL THROW NULL POINTER EXCEPTION
        for (ConstraintViolation<SimpleCommand> violation : contstraintViolations){
            System.out.println(violation.getMessage());
        }
    }
}

--------

Then, from a JAX-RS controller, I call this validator

------

SimpleCommand cmd = new SimpleCommand(req.getParameterMap());
        cmd.validate();
-----

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira


More information about the jboss-jira mailing list