访问 Jersey Resource 类中的外部对象

发布于 2024-12-06 10:01:19 字数 2085 浏览 1 评论 0原文

我有一个场景,我有以下嵌入式码头服务器:

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

并有以下资源类(使用 Jersey 框架)

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

在我的资源类中,我想访问一个对象 "myobj" 。有人可以建议我如何访问它吗?因为资源类是由框架直接调用的。

[编辑] 基本上我想知道如何将任何对象注入资源类?

[编辑]

我尝试了这个:

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

以下是提供程序类

  @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;
   }

}

,我在资源类中使用它:

 @Context

InjectZk zk;

我在运行服务器时收到以下错误:

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

有什么建议吗? (顺便说一句,我正在使用嵌入式码头)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2024-12-13 10:01:19

您可以通过编写自己的 InjectableProvider 和 Injectable 实现并将它们注册为应用程序中的提供程序来注入东西。有关如何实现此类提供程序的示例,您可以检查 SingletonTypeInjectableProviderPerRequestTypeInjectableProvider 是您可以用来实现该功能的辅助类,OAuthProviderInjectionProvider 的具体实现示例单例类型提供者。

示例代码:

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

您应该能够使用 @Context 注释来注入 Myobj 实例。

@Path("/helloworld")
public class HelloWorldResource {
    @Context Myobj myClass;
    ....
}

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;
    ....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文