I found an article that explains how to do this:
The problem here is, that CDI isn’t in place to instantiate the dependency. Their[sic] are two solutions for this problem:
- Let CDI instantiate the dependency, but let Jersey managed it This can be achived using
@ManagedBean
and a Jersey specific annotation.- Let CDI instantiate the dependency and let CDI manage it. This can be achieved using
@RequestScoped
or other CDI specific annotations.
I chose the first option and put the javax.annotation.ManagedBean
annotation on my resource. Here's an example:
package com.coderskitchen.thegreeter.rest;
import com.coderskitchen.thegreeter.greetings.GreetingService;
import javax.annotation.ManagedBean;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@Path("/greet")
@ManagedBean
public class Greeter {
@Inject
GreetingService gs;
@GET
@Path("{name}")
public String greetSomeone(@PathParam("name") String name) {
return gs.greetSomeone(name);
}
}
* Also I found this official article, which actually isn't as useful: http://docs.oracle.com/javaee/7/tutorial/doc/jaxrs-advanced004.htm