[jboss-jira] [JBoss JIRA] (WFLY-3088) Cannot Inject Validator using @Inject
Jozef Hartinger (JIRA)
issues at jboss.org
Mon Jun 1 05:47:02 EDT 2015
[ https://issues.jboss.org/browse/WFLY-3088?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Jozef Hartinger resolved WFLY-3088.
-----------------------------------
Resolution: Incomplete Description
Closing due to inactivity of the reporter. Feel free to reopen with more information.
> Cannot Inject Validator using @Inject
> -------------------------------------
>
> Key: WFLY-3088
> URL: https://issues.jboss.org/browse/WFLY-3088
> Project: WildFly
> Issue Type: Bug
> Components: CDI / Weld
> Affects Versions: 8.0.0.Final
> Reporter: Sat B
> Assignee: Eduardo Martins
>
> 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 was sent by Atlassian JIRA
(v6.3.15#6346)
More information about the jboss-jira
mailing list