使用Guice在Servlet应用程序中发布JAX-WS Web服务 [英] Publishing JAX-WS Webservice with Guice in a Servlet Application

查看:82
本文介绍了使用Guice在Servlet应用程序中发布JAX-WS Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们当前正在将现有的JBoss EJB应用程序移植到纯servlet解决方案中,该解决方案应该在Jetty中运行(我们当前使用的是版本6,但该版本几乎不相关),并且使用Guice进行依赖项注入和AOP. .尽管复杂性很高,但到目前为止我们已经取得了很大的成功.持久层和我们的大多数服务都已启动并正在运行,包括JAX-RS REST服务.

We are currently porting an existing JBoss EJB application to a pure servlet solution which is supposed to run in a Jetty (we're currently using version 6, but the version is mostly irrelevant) and which uses Guice for dependency injection and AOP. Despite the huge complexity we've been quite succesfully so far. The persistence layer and most of our services are up and running, including JAX-RS REST services.

但是,当我们开始移植现有的JAX-WS SOAP服务时,我们遇到了困难.我们已经花了大约一天的时间在网上搜索,看来很多人在几年前也遇到过同样的问题.但是,似乎没有人提供一个好的解决方案.

However, when we started porting our existing JAX-WS SOAP services we ran into difficulties. We already spent about a day searching the web and it seems that many people ran into the same issues years ago. However, nobody seems to offer a good solution.

我们正在使用 GuiceServletContextListener 创建全局注入器已配置所有模块.至关重要的是我们只有一台喷油器,因为我们需要支持单例.

We're using a GuiceServletContextListener to create our global Injector which is configured with all the modules. It is vital that we only have one Injector because we need to support singletons.

据我们所知,具体的JAX-WS实现并不重要.我们目前正在试验Metro,但我们可能也可以使用Apache-CXF.

As far as we know, the concrete JAX-WS implementation is not relevant. We're currently experimenting with Metro, but we could probably just as well use Apache-CXF.

此stackoverflow帖子建议手动创建服务,然后通过Endpoint.publish(...)发布.但是,这不是可接受的解决方案,因为它不使用Jetty容器,而是启动自己的HTTP服务器.

This stackoverflow post suggests creating the service manually and then publishing it through Endpoint.publish(...). However, this is not an acceptable solution, because it does not use the Jetty container, but starts its own HTTP server.

final Module module = new HelloModule();
final Injector injector = Guice.createInjector(module);
final HelloService helloService = injector.getInstance(HelloService.class);
Endpoint.publish("http://localhost:8080/helloService", helloService);

相同的stackoverflow帖子还建议使用此线程完全相同的问题所在讨论过,但似乎没有人解决.

The same stackoverflow post also suggests using JAX-WS Guice integration which in general sounds like a good approach. However, this solution creates its own Injector and is therefore incompatible with our GuiceServletContextListener based approach. We found this thread where exactly the same problem is discussed, but nobody seems to have a solution.

我们还查看了 guice-cxf ,它应该进行集成将Apache-CXF集成到Guice应用程序中很容易,但是据我们所知,到目前为止,这仅适用于JAX-RS REST服务.

We also looked at guice-cxf which is supposed to make the integration of Apache-CXF into Guice applications easy, but as far as we understand the description, this only works for JAX-RS REST services so far.

为了避免在此上浪费更多的时间并尝试重新发明轮子,我们在这里发布了此问题,希望其他人已经度过了难关,并可以为我们提供一些指导甚至是一些可行的示例.任何帮助,我们将不胜感激.

To not waste any more time on this and trying to reinvent the wheel we posted this question here in the hope that someone else has already been through this hell and can give us some pointers or maybe even some working examples. Any help is greatly appreciated.

推荐答案

我们终于通过扩展

We finally solved it quite elegantly by extending CXFNonSpringServlet. You simply override the loadBus() method where you can configure all your service endpoints.

@Singleton
public class SoapServlet extends CXFNonSpringServlet {
    private static final long serialVersionUID = 1L;

    private final SomeFacade someFacade;

    @Inject
    SoapMachineServlet(final SomeFacade someFacade) {
        this.someFacade = someFacade;
    }

    @Override
    public void loadBus(ServletConfig servletConfig) throws ServletException {
        super.loadBus(servletConfig);

        Bus bus = getBus();
        BusFactory.setDefaultBus(bus);
        Endpoint.publish("/SomeFacade", someFacade);
    }
}

该类本身只是一个servlet,然后可以使用进行绑定ServletModule :

The class itself is a simply a servlet, which can then be bound using a ServletModule:

public class SomeModule extends ServletModule {
    @Override
    protected void configureServlets() {
        serve("/some/path*").with(SoapServlet.class);
    }
}

这篇关于使用Guice在Servlet应用程序中发布JAX-WS Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆