18

I am using ejb 3 and trying to @Inject HttpServletRequest, but while deploying I occur exception.

Code:

@Inject private HttpServletRequest httpRequest;

Exception:

org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [HttpServletRequest] with qualifiers [@Default] at injection point [[field] @Inject private com.kmware.ttk.highway.beans.session.UserSessionBean.httpRequest]

What could I do with that?

2 Answers 2

26

The lifecycle of HttpServletRequest is managed by the EJB/web container, not the CDI container. Attempting to inject it leads to issues because there are typically many implementations of the interface,and your CDI container does not have enough information to make a decision on which implementation to inject. Even if you successfully injected an instance of it, it would not be the same instance as being managed by the EJB container.

To acquire a properly managed instance of the request, do this instead:

@Context
private HttpServletRequest httpRequest;
6
  • 2
    Did you mean @javax.ws.rs.core.Context ?
    – user1679671
    Nov 16, 2012 at 16:19
  • 1
    @KirillBazarov - yes, thats the annotations to use.
    – Perception
    Nov 16, 2012 at 16:34
  • @KirillBazarov - did this solve your problem? If so feel free to upvote/accept this answer. Thanks.
    – Perception
    Nov 16, 2012 at 18:14
  • 33
    Noted should be that @Context works only inside a JAX-RS webservice class, and definitely don't work inside an EJB class. Perhaps the OP is coincidentally actually using JAX-RS as well even though that was nowhere mentioned in the question. Therefore, this answer looks at first glance a completely blind guess and an ignorant shoot in the dark, and after all a lucky shot.
    – BalusC
    Aug 12, 2013 at 16:51
  • 4
    Nah, when you've been working a web service implementation for a year you kind of recognize when someone else is running into the same problems as you did early in the process.
    – Perception
    Aug 12, 2013 at 21:12
10

If your dependent is a JAX-RS (Restful) class then note the answer above. On the other hand, if you've got a more complex arrangement of dependency injection the question is certainly valid.

This capability has been added to the CDI 1.1 specification (JSR-346) which in turn has been added to the new Java EE 7 specification. In other words, the newest class of Java Enterprise Application servers will be able to handle this.

In the meantime, if you need to be able to manage some request scopes dependencies that in term need access to the actual HttpServletRequest information, you can use your approach and use the JBoss Solder tool. (Don't panic if the web site looks defunct, the fact of the matter is the work got shifted to the official CDI 1.1 spec implementation—i.e. "Weld 2"—so they aren't working on solder any more. But it's still perfectly suitable for CDI 1.0.)

The maven dependencies would be

<dependency>
  <groupId>org.jboss.solder</groupId>
  <artifactId>solder-api</artifactId>
  <version>3.2.0.Final</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.jboss.solder</groupId>
  <artifactId>solder-impl</artifactId>
  <version>3.2.0.Final</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.