Apache CXF + Spring Java config (no XML) Apache CXF + Spring Java config (no XML) spring spring

Apache CXF + Spring Java config (no XML)


Everything posted here is not 100% XML configuration free - all posts are using the classpath:META-INF/cxf/cxf.xml, which is also used in most tutorials on the web. But thereĀ“s a solution for that: Define a org.apache.cxf.bus.spring.SpringBus as @Bean and configure name = Bus.DEFAULT_BUS_ID, comming from org.apache.cxf.Bus.

As described in the other answers, the org.apache.cxf.jaxws.EndpointImpl has to be instantiated - including forwarding of the Beans SpringBus and the SEI-implementing Class. Also, the publish()-Method of EndpointImpl has to becalled, including a String containing an URL ending:

package de.jonashackt.tutorial.configuration;import javax.xml.ws.Endpoint;import org.apache.cxf.Bus;import org.apache.cxf.bus.spring.SpringBus;import org.apache.cxf.jaxws.EndpointImpl;import org.apache.cxf.transport.servlet.CXFServlet;import org.springframework.boot.context.embedded.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import de.codecentric.namespace.weatherservice.WeatherService;import de.jonashackt.tutorial.endpoint.WeatherServiceEndpoint;@Configurationpublic class WebServiceConfiguration {    @Bean    public ServletRegistrationBean dispatcherServlet() {        return new ServletRegistrationBean(new CXFServlet(), "/soap-api/*");    }    @Bean(name = Bus.DEFAULT_BUS_ID)    public SpringBus springBus() {        return new SpringBus();    }        @Bean    public WeatherService weatherService() {        return new WeatherServiceEndpoint();    }    @Bean    public Endpoint endpoint() {        EndpointImpl endpoint = new EndpointImpl(springBus(), weatherService());        endpoint.publish("/WeatherSoapService");        return endpoint;    }}

If you want to learn more about Apache CXF together with SpringBoot, I recommend a look on this github project.


All that's needed is a endpoint.publish() call above.


This thread definitely put me on the right track to getting CXF to run in pure Spring Java configuration, but it didn't provide everything that is required.

For my self, pure Java configuration means without a web.xml file, which I think this answer assumes is present. Spring Boot for example doesn't use a web.xml file.

So to register a CXF endpoint without the use of any XML files at all you will need a configuration file that also loads the CXFServlet.

import org.apache.cxf.Bus;import org.apache.cxf.jaxws.EndpointImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.ImportResource;import javax.xml.ws.Endpoint;@Configuration@ImportResource({"classpath:META-INF/cxf/cxf.xml"})public class JavaConfiguration {    @Autowired    private Bus bus;    @Bean    public Endpoint myServiceEndpoint() {        EndpointImpl endpoint = new EndpointImpl(bus, new MyService());        endpoint.publish("/myservice");        return endpoint;    }    @Bean    public ServletRegistrationBean cxfServlet() {        ServletRegistrationBean servlet = new ServletRegistrationBean(new CXFServlet(), "/services/*");        servlet.setLoadOnStartup(1);        return servlet;    }}

The above is all the configuration required to successfully load a CXF endpoint within Spring.

I have also created a small project that demonstrates this.