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, logInInterceptor & logOutInterceptor, 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
cxf.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:cxf="http://cxf.apache.org/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:endpoint id="changeStudent" implementor="com.student.ChangeStudentDetailsImpl" address="/ChangeStudent"> <jaxws:features> <bean class="org.apache.cxf.feature.LoggingFeature"> <property name="prettyLogging" value="true" /> </bean> </jaxws:features> </jaxws:endpoint> </beans>
Run Client
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();
factory.setServiceClass(ChangeStudentDetails.class);
factory.setAddress("http://localhost:8080/CXFTutorial/ChangeStudent?wsdl");
ChangeStudentDetails client = (ChangeStudentDetails) factory.create();
Student student = new Student();
student.setName("Rockey");
Student changeName = client.changeName(student);
System.out.println("Server said: " + changeName.getName());
System.exit(0);
}
}
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.
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.