简体   繁体   English

使用jersey-spring3和现有的Spring应用程序上下文来支持Web服务

[英]Rest web-services with jersey-spring3 and existing Spring application context

I have an existing Spring application with my own context that gets bootstrapped from douzens of Spring xml files. 我有一个现有的Spring应用程序,它有自己的上下文,可以从几十个Spring xml文件中引导。 A Grizzly web-server is started to publish Soap services. Grizzly Web服务器开始发布Soap服务。

Now I would like to also serve Rest requests from the same Grizzly. 现在我也想从同一个Grizzly那里提供休息请求。 I'm using jersey-spring3 but it starts it's own, separate application context from a required applicationContext.xml. 我正在使用jersey-spring3,但它从所需的applicationContext.xml启动它自己独立的应用程序上下文。

This is the code that creates the Grizzly HttpServer where Rest and Soap web-services are registered: 这是创建Grizzly HttpServer的代码,其中注册了Rest和Soap Web服务:

    //rest services
    ResourceConfig resourceConfig = new ResourceConfig(
            RestService1.class, //these are 
            RestService2.class //jersey-spring services
    );
    HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create("http://localhost:8080/rest"), resourceConfig, false);

    //soap services
    HttpHandler httpHandler = new JaxwsHandler(mySoapWebService, false);
    httpServer.getServerConfiguration().addHttpHandler(httpHandler, myPath);

    httpServer.start();

My Rest services (created by the 2nd Spring context) have injection dependencies from the first app context. My Rest服务(由第二个Spring上下文创建)具有来自第一个应用程序上下文的注入依赖项。 These injections obviously don't work. 这些注射显然不起作用。 Curreetly I'm injecting them myself manually with some hacky code. Curreetly我自己用一些hacky代码手动注入它们。

What is the correct way to inject Spring services for Rest request handling in an existing application, where jersey re-uses the existing context? 在现有应用程序中为Rest请求处理注入Spring服务的正确方法是什么,其中jersey重新使用现有上下文?

Take a look at the helloworld-spring-annotations from the Jersey project on GitHub. 看一下GitHub上Jersey项目的helloworld-spring-annotations You just need to set the "contextConfig" property in the Jersey application with the value being the instance of the Spring ApplicationContext 您只需要在Jersey应用程序中设置"contextConfig"属性 ,其值为Spring ApplicationContext的实例

resourceConfig.property("contextConfig", 
        new AnnotationConfigApplicationContext(SpringAnnotationConfig.class));

Then you should be able to @Autowired your Spring components into your Jersey components. 然后你应该能够将你的Spring组件@Autowired到你的Jersey组件中。

See Also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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