The problem with the CDI line @httpparam

I follow Weld doc

in section 4.11. InjectionPoint Object

There is a very interesting example of how to get the http parameter using CDI

but I copied the code in netbeans, everything compiles, but has a deployment error

Called: org.jboss.weld.exceptions.DeploymentException: WELD-001408 The injection point has unsatisfied dependencies. Injection point: parameter 1 java.lang.String com.test.HttpParamProducer.getParamValue (javax.enterprise.inject.spi.InjectionPoint, javax.servlet.ServletRequest); Qualifiers: [@ javax.enterprise.inject.Default ()]

How to solve this problem???

public class HttpParamProducer { @HttpParam("") @Produces String getParamValue( InjectionPoint ip, ServletRequest request) { return request.getParameter(ip.getAnnotated().getAnnotation(HttpParam.class).value()); } } 
+4
source share
2 answers

after two years, it seems that this question is still interested

this is a short start to the CDI specification where the container is not required to display the HttpServletRequest as an injection bean

here is a reasonable work around

 @WebListener public class HttpServletRequestProducer implements ServletRequestListener { private final static ThreadLocal<HttpServletRequest> holder = new ThreadLocal<HttpServletRequest>(); @Override public void requestDestroyed(ServletRequestEvent sre) { holder.remove(); } @Override public void requestInitialized(ServletRequestEvent sre) { holder.set((HttpServletRequest)sre.getServletRequest()); } @Produces @RequestScoped HttpServletRequest get() { return holder.get(); } } 

now @Inject HttpServletRequest will work as expected

happy coding

0
source

Each parameter in the producer method is entered, and not one of your beans (including producers) provides a ServletRequest API type for this injection point.

0
source

All Articles