Trace SOAP request/response using JAX-WS

Trace SOAP request/response using JAX-WS explains about how to trace the raw XML request and response usng java's in-bult JAX-WS implementation
It is a life saver, when we dont know the exact request which we are passing to the server.
For deploying the service, you can follow this tutorial CXF Web Service Tutorial. After the deployment you can access the below url
http://localhost:8080/CXFTutorial/ChangeStudent?wsdl
Here I am generating Java Code From A WSDL Document using wsimport tool, So that client can Invoke/Consume the Service
Required Libraries
You need to download
- JDK 7 (wsimport tool is available from jdk5 onwards)
You can view the generated code on below screenshot
Run Client
Below code you can see the bold characters, which is used for tracing the SOAP request/response
package com.student;
import com.student.ChangeStudentDetails;
import com.student.ChangeStudentDetailsImplService;
import com.student.Student;
// Trace XML request/response using JAX-WS
public class Main {
public static void main(String[] args) {
//For tracing the SOAP message
System.setProperty("com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump", "true");
System.setProperty("com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump", "true");
System.setProperty("com.sun.xml.ws.transport.http.HttpAdapter.dump", "true");
System.setProperty("com.sun.xml.internal.ws.transport.http.HttpAdapter.dump", "true");
ChangeStudentDetailsImplService service = new ChangeStudentDetailsImplService();
ChangeStudentDetails changeStudentDetailsImplPort = service.getChangeStudentDetailsImplPort();
Student student = new Student();
student.setName("Rockey");
Student changeName = changeStudentDetailsImplPort.changeName(student);
System.out.println("Server said: " + changeName.getName());
}
}
import com.student.ChangeStudentDetails;
import com.student.ChangeStudentDetailsImplService;
import com.student.Student;
// Trace XML request/response using JAX-WS
public class Main {
public static void main(String[] args) {
//For tracing the SOAP message
System.setProperty("com.sun.xml.ws.transport.http.client.HttpTransportPipe.dump", "true");
System.setProperty("com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump", "true");
System.setProperty("com.sun.xml.ws.transport.http.HttpAdapter.dump", "true");
System.setProperty("com.sun.xml.internal.ws.transport.http.HttpAdapter.dump", "true");
ChangeStudentDetailsImplService service = new ChangeStudentDetailsImplService();
ChangeStudentDetails changeStudentDetailsImplPort = service.getChangeStudentDetailsImplPort();
Student student = new Student();
student.setName("Rockey");
Student changeName = changeStudentDetailsImplPort.changeName(student);
System.out.println("Server said: " + changeName.getName());
}
}
Output
---[HTTP request - http://localhost:8080/CXFTutorial/ChangeStudent]--- Accept: text/xml, multipart/related Content-Type: text/xml; charset=utf-8 SOAPAction: "" User-Agent: JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e <?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:changeName xmlns:ns2="http://student.com/"><arg0><name>Rockey</name></arg0></ns2:changeName></S:Body></S:Envelope>-------------------- ---[HTTP response - http://localhost:8080/CXFTutorial/ChangeStudent - 200]--- null: HTTP/1.1 200 OK Content-Length: 232 Content-Type: text/xml;charset=UTF-8 Date: Fri, 22 Jan 2016 12:42:01 GMT Server: Apache-Coyote/1.1 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:changeNameResponse xmlns:ns2="http://student.com/"><return><name>Hello Rockey</name></return></ns2:changeNameResponse></soap:Body></soap:Envelope>-------------------- Server said: Hello Rockey