Apache CXF-based Java Web Services: How to log/print the payload XML of incoming SOAP request using LoggingInInterceptor?


I’ve had a requirement to print the exact SOAP XML of incoming requests to one of our web services written using Apache CXF Framework and here’s what I did.

For doing this quickly, I’m taking the code from http://singztechmusings.wordpress.com/2010/12/10/java-web-services-soap-over-ssl-cxf-framework/ for CalculationServerIfc that basically does addition/subtraction based on web service requests.

Here’s it:

package test.calculationserver;

import javax.jws.WebService;

@WebService
public interface CalculationServerIfc {
public int add(int a, int b);
public int subtract(int a, int b);
}

and I’m assuming it’s up with this WSDL url – https://localhost:8443/calculationserver/CalculationServerImpl?wsdl

Now, I’m trying to hit this using SoapUI v2.5.1.

Here’s the SOAP request XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="http://calculatorws.test.singz/">
<soapenv:Header/>
<soapenv:Body>
<cal:add>
<arg0>3</arg0>
<arg1>3</arg1>
</cal:add>
</soapenv:Body>
</soapenv:Envelope>

and the SOAP response,

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:addResponse xmlns:ns2="http://calculatorws.test.singz/">
<return>6</return>
</ns2:addResponse>
</soap:Body>
</soap:Envelope>

My requirement is to log this SOAP XML at server side. So, I’m just rewriting the CalculationService code like this:

import javax.jws.WebService;
import org.apache.cxf.interceptor.InInterceptors;

@WebService
@InInterceptors(interceptors = "org.apache.cxf.interceptor.LoggingInInterceptor")
public interface CalculationServerIfc {
public int add(int a, int b);
public int subtract(int a, int b);
}

Now, when I hit it using SoapUI, here’s the log message I get in standard output:

Aug 26, 2011 2:14:31 PM org.apache.cxf.interceptor.AbstractLoggingInterceptor log
INFO: Inbound Message
—————————-
ID: 1
Address: /cxf/calculator-service
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {content-type=[text/xml;charset=UTF-8], host=[localhost:9999], Content-Length=[279], SOAPAction=[""], user-agent=[Jakarta Commons-HttpClient/3.1], Content-Type=[text/xml;charset=UTF-8]}
Payload: <soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:cal=”http://calculatorws.test.singz/”&gt;
<soapenv:Header/>
<soapenv:Body>
<cal:add>
<arg0>3</arg0>
<arg1>3</arg1>
</cal:add>
</soapenv:Body>
</soapenv:Envelope>
————————————–

It, by default, uses Java’s logging mechanism. You can use log4j/slf4j instead. If you like to know more about this, visit http://cxf.apache.org/docs/debugging-and-logging.html.

About these ads

7 comments

  1. [...] Apache CXF-based Web Services: How to log/print the payload XML of incoming SOAP request using InInt… (singztechmusings.wordpress.com) [...]

  2. [...] Apache CXF-based Java Web Services: How to log/print the payload XML of incoming SOAP request using&… [...]

  3. [...] Apache CXF-based Web Services: How to log/print the payload XML of incoming SOAP request using InInt… (singztechmusings.wordpress.com) [...]

  4. [...] Apache CXF-based Web Services: How to log/print the payload XML of incoming SOAP request using InInt… (singztechmusings.wordpress.com) [...]

  5. [...] Apache CXF-based Web Services: How to log/print the payload XML of incoming SOAP request using InInt… (singztechmusings.wordpress.com) Share this:EmailPrintDiggStumbleUponFacebookTwitterRedditLinkedInLike this:LikeBe the first to like this post. [...]

  6. [...] Apache CXF-based Web Services: How to log/print the payload XML of incoming SOAP request using InInt… (singztechmusings.wordpress.com) [...]

  7. [...] Apache CXF-based Web Services: How to log/print the payload XML of incoming SOAP request using InInt… (singztechmusings.wordpress.com) [...]

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

Follow

Get every new post delivered to your Inbox.

Join 27 other followers

%d bloggers like this: