문제

I follow the Weld's doc

in the section 4.11. The InjectionPoint object

There is a very interesting example about how to obtain the http parameter using CDI

but i copy-pasted the code into netbeans, everything compiles, but has an deployment error

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Injection point has unsatisfied dependencies. Injection point: parameter 1 of 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());

   }
}
도움이 되었습니까?

해결책 2

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

this is a short coming of the CDI spec, where it doesn't require the container to expose HttpServletRequest as injectable 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 be working as expected

happy coding

다른 팁

Every parameter on a producer method is injected, and none of your beans (including producers) provides the API type ServletRequest to satisfy this injection point.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top