Search code examples
cxfjax-wsspring-boot

CXF with Spring-Boot


I am attempting to get CXF and Sprint Boot to play nicely. I have a JAX-WS service endpoint called SubscriberApi. Looking at the spring-boot logs I see successful mapping:

Mapping servlet: 'CXFServlet' to [/api/*]
Setting the server's publish address to be /SubscriberApi

However, I cant seem to get the WSDL when hitting:

http://localhost:8080/api/SubscriberApi?wsdl
@Configuration
@ImportResource({"classpath:META-INF/cxf/cxf.xml"})
public class CxfConfiguration  {
  @Bean
  public SubscriberApi subscriberApi() {
    return new SubscriberApi();
  }
  @Bean
  public ServletRegistrationBean servletRegistrationBean() {
    CXFServlet cxfServlet = new CXFServlet();

    ServletRegistrationBean servletRegistrationBean =
        new ServletRegistrationBean(cxfServlet, "/api/*");
    servletRegistrationBean.setLoadOnStartup(1);
    return servletRegistrationBean;
  }
  @DependsOn("servletRegistrationBean")
  @Bean
  public Endpoint jaxwsEndpoint(SubscriberApi subscriberApi){
    javax.xml.ws.Endpoint jaxwsEndpoint =
        javax.xml.ws.Endpoint.publish("/SubscriberApi", subscriberApi);
      return jaxwsEndpoint;
  }
 }

Solution

  • Have your jaxwsEndpoint bean return an instance of org.apache.cxf.jaxws.EndpointImpl, which extends javax.xml.ws.Endpoint:

    @Autowired
    private ApplicationContext applicationContext;
    
    @DependsOn("servletRegistrationBean")
    @Bean
    public Endpoint jaxwsEndpoint(){
       Bus bus = (Bus) applicationContext.getBean(Bus.DEFAULT_BUS_ID);
       EndpointImpl endpoint = new EndpointImpl(bus, subscriberApi());
       endpoint.publish("/SubscriberApi");
       // also showing how to add interceptors
       endpoint.getServer().getEndpoint().getInInterceptors().add(new LoggingInInterceptor());
       endpoint.getServer().getEndpoint().getOutInterceptors().add(new LoggingOutInterceptor());
    
       return endpoint;
    }
    

    The original post doesn't include a runnable example, but this should solve the issue.

    A running example can be found here, with all the configuration linked together: Application.java