Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use CDI to @Inject a class in Jersey 1.x?

I think I'm asking this question but for Jersey 1.x: Dependency injection with Jersey 2.0

I'm using Glassfish 3, CDI and Jersey 1.x. I have a @WebService that is injecting a class like this:

@Inject
Foo foo;

I've tested this in the @WebService and it works. But the same line of code in my Jersey resource throws a NPE when it tries to use foo. I think Jersey 1.x is ignoring the CDI annotations. How can I get dependency injection working like it does in my @WebService?

Foo is a pojo and my web.xml is using the ServletContainer:

<servlet>
    <servlet-name>JerseyServlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

I've found some help here. The problem is my Foo @Injects its own beans (they're actually EJBs that come from a class with @Provides in it). resourceContext.getResource(Foo.class); returns an instance of Foo, but foo's @Injected fields are null.

like image 596
Daniel Kaplan Avatar asked Apr 10 '14 16:04

Daniel Kaplan


People also ask

How does @inject annotation work?

Injectable constructors are annotated with @Inject and accept zero or more dependencies as arguments. @Inject can apply to at most one constructor per class. @Inject is optional for public, no-argument constructors when no other constructors are present. This enables injectors to invoke default constructors.

What is the use of @inject in spring boot?

@Inject and @Autowired both annotations are used for autowiring in your application. @Inject annotation is part of Java CDI which was introduced in Java 6, whereas @Autowire annotation is part of spring framework. Both annotations fulfill same purpose therefore, anything of these we can use in our application. Sr.

What does @inject annotation mean?

The @Inject annotation lets us define an injection point that is injected during bean instantiation. Injection can occur via three different mechanisms. Bean constructor parameter injection: public class Checkout { private final ShoppingCart cart; @Inject.


1 Answers

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:

  1. Let CDI instantiate the dependency, but let Jersey managed it This can be achived using @ManagedBean and a Jersey specific annotation.
  2. 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

like image 143
Daniel Kaplan Avatar answered Sep 30 '22 04:09

Daniel Kaplan