4

在启动嵌入式 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"/>
4

2 回答 2

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);
于 2016-10-28T13:23:23.663 回答
-1

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

您应该添加此排除项:

<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>
于 2014-11-11T11:29:49.740 回答