title: How to properly integrate a CXF client into Spring Boot
body: <p>I was looking at various ways how to build a SOAP client with CXF into a Spring Boot application. In particular I'm interested in configuring request/response logging.</p>
  
  <p>Precondition: the CXF Maven plugin is used to generate Java stubs from the WSDL files.</p>
  
  <p>I studied a number of tutorials [1][2][3] and they all do that slightly differently. The question is whether there's an "officially endorsed" way of integrating a CXF client with Spring Boot. The CXF documentation doesn't seem to say.</p>
  
  <p><strong>Option 1: BindingProvider</strong></p>
  
  <p>The existing code I'm working with (not mine) does it like that:</p>
  
  <pre><code>@Bean
  public PartnerServicePortType partnerServicePortType() {
    PartnerServicePortType partnerServicePortType = new PartnerServiceV0().getPartnerService();
    configureService((BindingProvider) partnerServicePortType, Services.PARTNER_SERVICE.serviceName());
    return partnerServicePortType;
  }
  
  private void configureService(BindingProvider bindingProvider, String path) {
    bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, baseUrl + path);
    if (!StringUtils.isEmpty(username)) {
      bindingProvider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
      bindingProvider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
    }
  
    /* add logging interceptors
    Client client = ClientProxy.getClient(bindingProvider);
    Endpoint cxfEndpoint = client.getEndpoint();
  
    cxfEndpoint.getInInterceptors().add(new PayloadExtractingLoggingInInterceptor());
    cxfEndpoint.getInFaultInterceptors().add(new PayloadExtractingLoggingInInterceptor());
    cxfEndpoint.getOutInterceptors().add(new PayloadExtractingLoggingOutInterceptor());
    cxfEndpoint.getOutFaultInterceptors().add(new PayloadExtractingLoggingOutInterceptor());
    */
  }
  </code></pre>
  
  <p><code>PartnerServiceV0</code> is generated class that extends <code>javax.xml.ws.Service</code> and looks like this:</p>
  
  <pre><code>@WebServiceClient(name = "PartnerService_v0", 
                    wsdlLocation = "classpath:some.wsdl",
                    targetNamespace = "urn:some-namespace") 
  public class PartnerServiceV0 extends Service {
  </code></pre>
  
  <p>As you can see I added the code to enable request/response logging to the <code>configureServcie</code> method. While this <em>does</em> work it feels somewhat odd that we'd have to go through all of that for every service.</p>
  
  <p><strong>Option 2: JaxWsProxyFactoryBean</strong></p>
  
  <p>For verification purposes I ported the above code to using <code>JaxWsProxyFactoryBeans</code>:</p>
  
  <pre><code>@Bean(name = "partnerService")
  public PartnerServicePortType generateProxy() {
  
    JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
    proxyFactory.setServiceClass(PartnerServicePortType.class);
    proxyFactory.setAddress(baseUrl + Services.PARTNER_SERVICE.serviceName());
    if (!StringUtils.isEmpty(username)) {
      proxyFactory.setUsername(username);
      proxyFactory.setPassword(password);
    }
    return (PartnerServicePortType) proxyFactory.create();
  }
  </code></pre>
  
  <p>Adding logging config is something I'd still have to figure out with this strategy.</p>
  
  <p><strong>Tutorials I checked</strong></p>
  
  <ol>
  <li><a href="https://blog.codecentric.de/2016/07/spring-boot-apache-cxf-logging-monitoring-logback-elasticsearch-logstash-kibana/" rel="noreferrer">https://blog.codecentric.de/2016/07/spring-boot-apache-cxf-logging-monitoring-logback-elasticsearch-logstash-kibana/</a></li>
  <li><a href="http://www.baeldung.com/apache-cxf-with-spring" rel="noreferrer">http://www.baeldung.com/apache-cxf-with-spring</a></li>
  <li><a href="http://www.littlebigextra.com/consume-secure-soap-web-service-spring-boot-application/" rel="noreferrer">http://www.littlebigextra.com/consume-secure-soap-web-service-spring-boot-application/</a></li>
  </ol>