I have a question about setting up CXF to provide RESTful endpoints through 
Guice.

Is this along the right path?  

Should I be injecting all of my resources like this or should I set each one up 
individually using a new JAXRSServerFactoryBean?

Inject all of my resources using a single JAXRSServerFactoryBean:

                @Inject
                @WebResource
                private Map<String, Provider<Object>> resources;

                @Override
                protected void loadBus(ServletConfig sc)
                {
                        super.loadBus(sc);

                        final JAXRSServerFactoryBean sf = new 
JAXRSServerFactoryBean();
                        sf.setBus(getBus());
                        sf.setAddress("/");
                        sf.setProvider(new JacksonJsonProvider());
                        sf.setResourceProviders(…);

                        sf.create();
                }

Inject each of my resources using a new JAXRSServerFactoryBean:

                for (String key : resources.keySet())
                {
                        try
                        {
                                LOG.debug("------------------ {}", key);
                                final Provider<?> bean = resources.get(key);
                                final JAXRSServerFactoryBean sf = new 
JAXRSServerFactoryBean();
                                sf.setBus(getBus());
                                sf.setAddress(key);
                                sf.setProvider(new JacksonJsonProvider());
                                sf.setResourceProvider(new 
SingletonResourceProvider(bean.get()));
                                sf.create();
                        }
                        catch (Exception x)
                        {
                                LOG.error("Cannot start endpoint: ", 
x.getMessage());
                        }
                }

It looks like when I do it this way it will iterate through all of the 
resources for a match where as when I set up each endpoint individually it 
matches that one only.  The problem I had with setting each up individually was 
that I could not get the “setAddress(“http://localhost:8080/ 
<http://localhost:8080/>“)” and the root resources @Path(“some-root”) to work 
properly…i ended up with the path twice (because I set the address to 
“/some-root”).

Also, what is the difference between returning a ResourceProvider vs. 
setServiceBean?


Reply via email to