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

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

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();

    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.

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 *