简体   繁体   中英

How to configure existing Application Context in Jersey-Spring using Grizzly embedded server?

When starting an embedded Grizzly server, I would like Jersey to use my existing Spring container. This was possible in jersey-1.0 ( Grizzly, sharing spring generated context ), but does not seem to work in Jersey 2.x.

This code works in 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();

Here is my code in Jersey 2 (I can't figure out a replacement for 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, "");

When I run the code (with jersey-spring3), I get this exception:

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 is trying to load my Spring context from applicationContext.xml, but it is annotation based and already loaded. How can I get Grizzly and/or Jersey to find my context?

IVY Config is:

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

I had exactly the same issue... You just need to link your existing context with Grizzly. In my case 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);

Sorry to answer that late but can maybe useful for someone else.

You should add this exclusion :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM