JAX-RS Jersey Injection Instance

I am trying to insert a resource instance into a JAX-RS application with a Singleton scope, however, when I do this, I get:

WARNING. The provider com.test.jersey.app.MyResource, registered in the SERVER runtime, does not implement any provider interfaces applicable at the SERVER runtime. Due to configuration restrictions, the provider com.test.jersey.app.MyResource will be ignored.

I have an application as shown below 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 accomplished 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 AbstractBinder but could not find a way to bind the MyResource instance 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>
+4
1

DI,

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);

. :

+3

Source: https://habr.com/ru/post/1659700/


All Articles