在启动嵌入式 Grizzly 服务器时,我希望 Jersey 使用我现有的 Spring 容器。这在 jersey-1.0(Grizzly,共享 spring 生成的上下文)中是可能的,但在 Jersey 2.x 中似乎不起作用。
此代码适用于 Jersey 1.17:
HttpServer server = new HttpServer();
final NetworkListener listener = new NetworkListener("grizzly", NetworkListener.DEFAULT_NETWORK_HOST, PACS.RESTPort);
listener.setSecure(false);
server.addListener(listener);
ResourceConfig rc = new PackagesResourceConfig("edu.mayo.qia.pacs.rest");
// Let Jersey know about our existing context
SpringComponentProviderFactory handler = new SpringComponentProviderFactory(rc, PACS.context);
HttpHandler processor = ContainerFactory.createContainer(HttpHandler.class, rc, handler);
server.getServerConfiguration().addHttpHandler(processor, "");
server.start();
这是我在 Jersey 2 中的代码(我无法找到 SpringComponentProviderFactory 的替代品):
HttpServer server = new HttpServer();
final NetworkListener listener = new NetworkListener("grizzly", NetworkListener.DEFAULT_NETWORK_HOST, PACS.RESTPort);
server.addListener(listener);
ResourceConfig rc = new ResourceConfig();
rc.packages("edu.mayo.qia.pacs.rest");
HttpHandler processor = ContainerFactory.createContainer(GrizzlyHttpContainer.class, rc);
server.getServerConfiguration().addHttpHandler(processor, "");
当我运行代码(使用 jersey-spring3)时,我得到了这个异常:
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from class path resource [applicationContext.xml];
nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
Grizzly 正在尝试从 applicationContext.xml 加载我的 Spring 上下文,但它是基于注释的并且已经加载。如何让 Grizzly 和/或 Jersey 找到我的上下文?
常春藤配置是:
<dependency org="org.glassfish.jersey.core" name="jersey-client" rev="2.3"/>
<dependency org="org.glassfish.jersey.core" name="jersey-server" rev="2.3"/>
<dependency org="org.glassfish.jersey.core" name="jersey-common" rev="2.3"/>
<dependency org="org.glassfish.jersey.connectors" name="jersey-grizzly-connector" rev="2.3"/>
<dependency org="org.glassfish.jersey.containers" name="jersey-container-grizzly2-http" rev="2.3"/>
<dependency org="org.glassfish.jersey.ext" name="jersey-spring3" rev="2.3"/>