Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey JAX-RS Instance injection

I'm trying to inject an instance of a resource into a JAX-RS application with Singleton scope, however when I do this I get:

WARNING: A provider com.test.jersey.app.MyResource registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.test.jersey.app.MyResource will be ignored.

I have an app as follows, that needs an already running instance of MyResource:

public class MyApp extends ResourceConfig {
  public MyApp(MyResource res) {
    super(
        MyService.class
        );
    registerInstances(res);
  }
}

And

public class MyResource {
  String instanceVar;

  public MyResource(String test) {
    instanceVar = test;
  }

  public String getString() {
    return instanceVar;
  }
}

With service:

@Path("/service")
public class MyService {
  @GET
  @Path("")
  public String get(@Context MyResource res) {
    String output;
    if (res != null) {
      output = res.getString();
    } else {
      output = "NOT SET";
    }
    return "output: " + output;
  }
}

This is being run using the following:

public static void main(String[] args) {
  MyResource resource = new MyResource("foo");
  MyApp restApp = new MyApp(resource);
  ServletHolder servlet = new ServletHolder(new ServletContainer(restApp));
  Server jettyServer = new Server(8080);
  ServletContextHandler context = new ServletContextHandler(jettyServer, "/*");
  context.addServlet(servlet, "/*");
  try {
    jettyServer.start();
    jettyServer.join();
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    jettyServer.destroy();
  }
}

I tried using an AbstractBinder, but couldn't find a way to bind the instance of MyResource to the service.

Dependencies:

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.23.2</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.23.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
        <version>2.23.2</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.23.2</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.2.19.v20160908</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-util</artifactId>
        <version>9.2.19.v20160908</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.2.19.v20160908</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-jmx</artifactId>
        <version>9.2.19.v20160908</version>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0</version>
    </dependency>
</dependencies>
like image 690
tmgstevens Avatar asked Nov 03 '16 09:11

tmgstevens


1 Answers

Use the DI system to make it injectable

final MyResource resource = new MyResource(...);
final AbstractBinder binder = new AbstractBinder() {
    @Override
    public void configure() {
        bind(resource).to(MyResource.class);
    }
};
final MyApp app = new MyApp();
app.register(binder);

See also:

  • Dependency injection with Jersey 2.0
like image 139
Paul Samsotha Avatar answered Oct 21 '22 15:10

Paul Samsotha