Logging SOAP client messages from CXF in Spring Boot

Since getting this to work was more troublesome that it should have been, here are the details.

First, the following dependencies will be required (I am using 2.7.18):

<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-api</artifactId>
	<version>${cxf.runtime.version}</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http</artifactId>
	<version>${cxf.runtime.version}</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-frontend-jaxws</artifactId>
	<version>${cxf.runtime.version}</version>
</dependency>

Next, we need a Spring configuration class that sets up the required CXF classes:


@Configuration
@ImportResource({ "classpath:META-INF/cxf/cxf.xml", "classpath:META-INF/cxf/cxf-extension-soap.xml" })
public class CxfConfig {

	@Autowired
	private SpringBus springBus;

	@PostConstruct
	public void activateLoggingFeature() {
           springBus.getInInterceptors().add(logInInterceptor());
    	   springBus.getInFaultInterceptors().add(logInInterceptor());
    	   springBus.getOutInterceptors().add(logOutInterceptor());
    	   springBus.getOutFaultInterceptors().add(logOutInterceptor());
	}

	@Bean
	public LoggingFeature loggingFeature() {
    	   LoggingFeature logFeature = new LoggingFeature();
    	   logFeature.setPrettyLogging(true);
    	   logFeature.initialize(springBus);
    	   springBus.getFeatures().add(logFeature);
    	   return logFeature;
	}

	public AbstractLoggingInterceptor logInInterceptor() {
	    return new LoggingInInterceptor();
	}

	public AbstractLoggingInterceptor logOutInterceptor() {
		LoggingOutInterceptor logOutInterceptor = new LoggingOutInterceptor();
		logOutInterceptor.setPrettyLogging(true);
		return logOutInterceptor;
	}

}

Finally, we need to add CXF components to the logging framework config (e.g. logback.xml)

...
   <logger name="org.apache.cxf" level="INFO"/>
...
---------------------------
ID: 12
Address: ... something ....
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>

     ... data here ....

  </soap:Body>
</soap:Envelope>

--------------------------------------
Advertisements