【问题标题】:Rest web-services with jersey-spring3 and existing Spring application context使用 jersey-spring3 和现有的 Spring 应用程序上下文来休息 web 服务
【发布时间】:2015-02-02 12:15:07
【问题描述】:

我有一个现有的 Spring 应用程序,我自己的上下文被引导 来自数十个 Spring xml 文件。 一个 Grizzly 网络服务器开始发布 Soap 服务。

现在我还想为来自同一个 Grizzly 的 Rest 请求提供服务。 我正在使用 jersey-spring3 但它启动它是自己的,与所需的应用程序上下文分开 applicationContext.xml。

这是创建 Grizzly HttpServer 的代码,其中 Rest 和 Soap 网络服务 已注册:

    //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();

我的 Rest 服务(由第二个 Spring 上下文创建)具有来自第一个应用程序上下文的注入依赖项。 这些注射显然不起作用。目前,我正在使用一些 hacky 代码自己手动注入它们。

在现有应用程序中注入 Spring 服务以处理 Rest 请求的正确方法是什么,其中 jersey 重用了现有的上下文?

【问题讨论】:

    标签: java spring rest jersey-2.0 grizzly


    【解决方案1】:

    查看来自 GitHub 上 Jersey 项目的 helloworld-spring-annotations。您只需要在 Jersey 应用程序中 set the "contextConfig" property ,其值为 Spring 的实例 ApplicationContext

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

    那么你应该可以@Autowired你的 Spring 组件到你的 Jersey 组件中。

    另请参阅:

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 2011-07-19
      • 2013-11-23
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多