PrettyPrint With CXF

PrettyPrint With CXF explains about  How to formatting (readable way with whitespace & line breaks) the request and response using CXF framework

For every application logging is vital because it is a must for debugging, CXF framework have in-built feature for enabling logging of request / response 

If you are interested to add logs in separate file using log4j, you can find below article

Configure Log4j with CXF
Apache CXF Logging

PrettyPrint With CXF

Here I am going to re-use CXF Web Service Tutorial

For enabling PrettyPrint logging using Interceptor you need to add abstractLogInterceptor, logInInterceptorlogOutInterceptor, We also added a property <property name="prettyLogging" value="true" /> to abstractLogInterceptor.

You need to import xmlns:cxf namespace in order to use <cxf:bus>, please check the following changed cxf.xml


<beans xmlns=""
	xmlns:cxf="" xmlns:xsi=""
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<bean id="abstractLogInterceptor" abstract="true">
		<property name="prettyLogging" value="true" />
	<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"
		id="logInInterceptor" parent="abstractLogInterceptor" />
	<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"
		id="logOutInterceptor" parent="abstractLogInterceptor" />
			<ref bean="logInInterceptor" />
			<ref bean="logOutInterceptor" />
	<jaxws:endpoint id="changeStudent"
		implementor="com.student.ChangeStudentDetailsImpl" address="/ChangeStudent" />

In order to run the client, you need to add cxf libraries to class path, you can see this on below article

Create CXF Client Example

Run Client
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.student.ChangeStudentDetails;
import com.student.Student;

// PrettyPrint With CXF Example

public final class StudentClient {

public static void main(String args[]) throws Exception {

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

    ChangeStudentDetails client =
(ChangeStudentDetails) factory.create();
    Student student =
new Student();
    Student changeName = client.changeName
("Server said: " + changeName.getName());
Output Without PrettyPrint

Here you can see the Tomcat server started and showing the request/response payloads  in single line, it is very difficult to read.

PrettyPrint With CXF

Output With PrettyPrint

Here you can see the Tomcat server started and showing the request/response payloads. Here response is correctly formatted in human readable format.

PrettyPrint With CXF

Your email address will not be published. Required fields are marked *