如何使用Grizzly嵌入式服务器在Jersey-Spring中配置现有的应用程序上下文?

时间:2013-11-11 17:57:04

标签: java jersey grizzly jersey-2.0

启动嵌入式Grizzly服务器时,我希望Jersey使用我现有的Spring容器。这在jersey-1.0(Grizzly, sharing spring generated context)中是可能的,但在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找到我的背景?

IVY配置是:

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

2 个答案:

答案 0 :(得分:2)

我有完全相同的问题......您只需要将现有的背景与Grizzly联系起来。就我而言ClassPathXmlApplicationContext

yourContext = new ClassPathXmlApplicationContext("application-context.xml");

ResourceConfig rc = new ResourceConfig(HazelcastRestService.class);
rc.property("contextConfig", yourContext);

URI uri = new JerseyUriBuilder().host("0.0.0.0").port(8888).build();
httpServer = GrizzlyHttpServerFactory.createHttpServer(uri, rc);

答案 1 :(得分:0)

很抱歉很晚才回答,但对其他人可能有用。

您应该添加此排除项:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.2</version>
                <configuration>
                    <classpathDependencyExcludes>
                        <classpathDependencyExcludes>org.glassfish.jersey.ext:jersey-spring3</classpathDependencyExcludes>
                    </classpathDependencyExcludes>
                </configuration>
            </plugin>
        </plugins>
    </build>