Access external objects in Jersey Resource class
Asked Answered
C

1

1

I have the scenario where I have the following embedded jetty server:

    Server server = new Server(8080);
      Context root = new Context(server, "/", Context.SESSIONS);
      root.addServlet(
            new ServletHolder(
                  new ServletContainer(
                        new PackagesResourceConfig(
                              "edu.mit.senseable.livesingapore.platform.restws.representations"))),
            "/");
Myobj myobj = new Myobj(12,13);
      server.start();

and have the following resource class (using Jersey framework)

    import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/")
public class DataStreams {
   @GET
   @Path("/datastreams")
   @Produces(MediaType.TEXT_PLAIN)
   public String getDataStreams() {
      return getStreams("text");
   }
}

Here in my resource class I want to access a object "myobj" . can someone suggest how I can access it? because the resource class in directly called by the framework.

[edit] Basically I want to know how to inject any object into resource class?

[Edit]

I tried this:

 pkgrc.getSingletons().add(
        new SingletonTypeInjectableProvider<Annotation, InjectZk>(
              InjectZk.class, new InjectZk(this.zooKeeper)) {
        });

following is the provider class

  @Provider
public class InjectZk {
   private ZooKeeper zk;

   public InjectZk() {
      // TODO Auto-generated constructor stub
   }

   public InjectZk(ZooKeeper zk) {
      // TODO Auto-generated constructor stub
      this.zk = zk;
   }

   public ZooKeeper getZk() {
      return zk;
   }

}

and I am using it in resource class as:

 @Context

InjectZk zk;

I am getting the following erorr while running the server:

SEVERE: Missing dependency for field: edu.mit.senseable.livesingapore.platform.core.components.clientrequest.InjectZk edu.mit.senseable.livesingapore.platform.core.components.clientrequest.DataStreams.zk
2011-09-28 16:18:47.714:/:WARN:  unavailable
com.sun.jersey.spi.inject.Errors$ErrorMessagesException

Any suggestions? ( BTW I am using Embedded jetty)

Cleliaclellan answered 22/9, 2011 at 7:8 Comment(0)
T
3

You can inject things by writing your own InjectableProvider and Injectable implementations and registering them as providers in your application. For an example of how such provider can be implemented you can check the SingletonTypeInjectableProvider or PerRequestTypeInjectableProvider which are helper classes you can use to implement that and OAuthProviderInjectionProvider for an example of a concrete implementation of a singleton type provider.

sample code:

    Server server = new Server(8080);
    Context root = new Context(server,"/",Context.SESSIONS);

    ResourceConfig rc = new PackagesResourceConfig("edu.mit.senseable.livesingapore.platform.restws.representations");
    rc.getSingletons().add(new SingletonTypeInjectableProvider<javax.ws.rs.core.Context, Myobj>(Myobj.class, new Myobj(12,13)){});

    root.addServlet(new ServletHolder(new ServletContainer(rc)), "/");
    server.start();

and you should be able to incject Myobj instance using @Context annotation.

@Path("/helloworld")
public class HelloWorldResource {
    @Context Myobj myClass;
    ....
}
Thumbstall answered 22/9, 2011 at 9:7 Comment(6)
can you suggest some sample code? I am sorry I am newbie in this field and also I am using embedded jetty with jerseyCleliaclellan
you can find basic InjectableProvider implementation in this threadMastermind
looks like Pavel edited my response and added the sample code - thanks, PavelThumbstall
@Pavel Bucek it doesnot work :( (is it mandatory that Myobj should be in sam package?) becasue I am getting following error:SEVERE: The following errors and warnings have been detected with resource and/or provider classes: SEVERE: Missing dependency for field: org.test.Myobj edu.mit.senseable.livesingapore.platform.core.components.clientrequest.DataStreams.myobjCleliaclellan
@Pavel Bucek hi any idea what I am missing?Cleliaclellan
damn.. sorry, there is a "minor" bug in code sample. SingletonTypeInjectableProvider must be initiated by following: new SingletonTypeInjectableProvider<javax.ws.rs.core.Context, Myobj>(Myobj.class, new Myobj(12,13)){}. I'll update the answer as well.Inelegant

© 2022 - 2024 — McMap. All rights reserved.