Search code examples
jsfcdihttp-request-parametersproducer

Weld exception when value for producer annotation is defined


I tried to implement solution from another SO question: https://stackoverflow.com/a/10059245/1943765 but it is not working in all cases.

When I put this code in CDI bean (bean code is shown on bottom):

@Inject @HttpParam
private String code; 

everything works fine. But when I try to define value for @HttpParam annotation (so, not default one) Weld is unable to start:

@Inject @HttpParam(value="code")
private String token;

I get this exception:

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type String with qualifiers @HttpParam
  at injection point [BackedAnnotatedField] @Inject @HttpParam private org.test.site.jsf.beans.request.ActivationBean.token
  at org.test.site.jsf.beans.request.ActivationBean.token(ActivationBean.java:0)
WELD-001475: The following beans match by type, but none have matching qualifiers:
  - Producer Method [String] with qualifiers [@HttpParam @Any] declared as [[BackedAnnotatedMethod] @Produces @HttpParam public org.test.site.cdi.producer.HttpParamProducer.getHttpParameter(InjectionPoint)]
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
    at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)
    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:63)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
    ... 4 more

The code I used is similar to linked SO question.

The custom @HttpParam annotation:

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER})
public @interface HttpParam {
    public String value() default "";
}

The annotation value producer:

public class HttpParamProducer {

    @Inject
    FacesContext facesContext;

    @Produces
    @HttpParam
    String getHttpParameter(InjectionPoint ip) {
        String name = ip.getAnnotated().getAnnotation(HttpParam.class).value();
        if ("".equals(name)) name = ip.getMember().getName();
        return facesContext.getExternalContext()
                .getRequestParameterMap()
                .get(name);
    }
}

And CDI bean using it is something like:

@Named("activation")
@RequestScoped
public class ActivationBean implements Serializable{

    @Inject
    @HttpParam(value="code")
    private String token;

    // rest of valid class code
}

Also I am using Tomcat 8 server with Weld Servlet 2.3.1.Final.

So.. what am I doing wrong? :-/


Solution

  • You need to add @Nonbinding to your value attribute so that its value does not necessarily select which producer method to invoke, but allows for a single factory style producer inspecting your injection point.

    You may also want to review what is the purpose of @Nonbinding annotation in a Qualifier supposed to be in Java EE7?