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>
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With