gpt4 book ai didi

java - Jersey JAX-RS 实例注入(inject)

转载 作者:搜寻专家 更新时间:2023-10-31 20:17:40 25 4
gpt4 key购买 nike

我正在尝试将资源实例注入(inject)到具有 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.

我有一个如下的应用程序,它需要一个已经运行的 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 的实例绑定(bind)到服务的方法。

依赖关系:

<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系统使其可注入(inject)

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

另请参阅:

关于java - Jersey JAX-RS 实例注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40397891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com