Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CXF and Google Guice using JAX-RS + JAX-WS

I would like to integrate CXF with Google Guice. I am already using Guice in my project and I want to avoid adding extra dependencies.

CXF was my choice because one of the requirements is to be able to provide XML, JSON, JSONP and SOAP interface to the users of the services without having duplicate code (right now we have SOAP-specific classes, for XML we use Struts and for JSON we wrote our own parsers, I know, I feel dirty too).

Anyway, afaik, CXF can fulfill this requirement, so it seems I'm stuck with CXF.

Any ideas or pointers or advice on how to integrate Guice with CXF? I thought of extending the CXFNonSpringJaxrsServlet class and hack Guice into it, but it somehow seems like I would be doing something that someone else has already done.

like image 384
chahuistle Avatar asked Jun 07 '11 09:06

chahuistle


3 Answers

With CXF 2.4.x, CXF no longer uses Spring to internally configure itself. Thus, you can do pretty much anything without Spring.

The configuration parts are definitely an issue. However, all the spring configuration stuff is just thin wrappers over the CXF API's. Thus, you can configure pretty much everything via the API's. You may just need to dig a little bit more.

As an example, with CXF 2.4, we now have started to support using Blueprint instead of Spring in OSGi. The blueprint support doesn't require any of the Spring stuff, but is modelled from it.

like image 119
Daniel Kulp Avatar answered Nov 18 '22 03:11

Daniel Kulp


Here is what I have been using (cxf-2.5.0).

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

    @Inject
    public WebServiceServlet(SamaService samaService) {
        this.samaService = samaService;
    }

    @Override
    protected void loadBus(ServletConfig sc) {
        super.loadBus(sc);
        BusFactory.setDefaultBus(getBus());
        Endpoint.publish("/Data.asmx", samaService);
    }
}
like image 42
natros Avatar answered Nov 18 '22 03:11

natros


You've got a problem. CXF is thoroughly dependent on Spring, due to it being internally configured by Spring and using Spring's facilities quite extensively. As such, I don't think it's practical to try to prise CXF out of Spring just so you can use Guice instead; at a minimum, you'd have to replicate all the configuration with custom Guice providers and it still will not work (parts of CXF use some of Spring's Web layer).

You could try using Spring's JavaConfig style for your code instead. That's mostly similar to the sorts of things that Guice does (as far as I can see). Or I suppose you could try using two separate DI frameworks in the same app… though that feels to me like an opportunity for “hilarious” failure.

like image 38
Donal Fellows Avatar answered Nov 18 '22 03:11

Donal Fellows