Jersey JAX-RS 实例注入

Jersey JAX-RS Instance injection

我试图将一个资源实例注入到具有 Singleton 作用域的 JAX-RS 应用程序中,但是当我这样做时,我得到:

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.

我有一个如下的应用程序,它需要一个已经 运行ning 的 MyResource 实例:

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

public class MyResource {
  String instanceVar;

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

  public String getString() {
    return instanceVar;
  }
}

有服务:

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

这是 运行 使用以下内容:

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

我尝试使用 AbstractBinder,但找不到将 MyResource 的 实例 绑定到服务的方法。

依赖关系:

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

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

另请参阅:

  • Dependency injection with Jersey 2.0