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

One thought on “Logging SOAP client messages from CXF in Spring Boot

  1. bless you with regard to the particular blog post ive really been searching regarding this kind of information on the web for sum time right now as a result cheers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s