http://qa.netbeans.org/modules/j2ee/promo-g/end2end/hello_ws.html
It is mostly desired to log the SOAP request and response XML into log file.
But there is no direct way, using which you can log the XML, because the XML generation part is in the control of the webservice framework. But the good news that you can configure it to print the XML where ever you like it to.
Configure logging of XML on server side using Netbeans
Creating web project
- Go to File - New Project - Web - Web Application
- Specify HelloWs as Project Name
- Specify project's directory
- Choose Java EE 5 as J2EE version and
- Click Finish
- Go to File - New File - Web services - Web Service - Next
- Specify name of web service, eg. HelloWebService
- Specify package for the web service, eg. org.netbeans.end2end.hellosample
- Click Finish
- Uncomment commented code in source file
- Add serviceName="GreeterWs" to WebService annotation
- Add operationName="sayHi" to WebMethod annotation
- Add @WebParam(name="name") to String param argument of method operation
- Add new sayHello(String): String operation to web service using Web Service -> Add Operation... action in editor
Implement both operation, so the implementation class will look like this:
- Go to File - New File - Web services - Message Handler - Next
- Specify name of message handler, eg. MessageHandler
- Specify package for the message handler, eg. org.netbeans.end2end.hellosample
- Click Finish
- Implement simple log method in handler, eg. like on following picture:
- And add handler to the webservice using Configure Handlers... action in context menu on HelloWebService's node
Configure logging of XML on client side using Netbeans
Write a myHandler as follows:
import java.io.IOException; import java.util.Set; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; import javax.xml.ws.handler.soap.SOAPMessageContext; public class MyHandler implements SOAPHandlerAnd write myHanlderResolver like this:{ public boolean handleMessage(SOAPMessageContext smc) { Boolean outboundProperty = (Boolean) smc .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); SOAPMessage message = smc.getMessage(); // This if condition gets reversed when we write // code on client side. if (outboundProperty.booleanValue()) { System.out.print(" SOAP Request "); } else { System.out.print(" SOAP Respone "); } try { message.writeTo(System.out); } catch (SOAPException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(""); // if this function will return true, only then the chaining concept will work. // if we return outboundProperty which happens to be false in some cases // the chaining will not work. //return outboundProperty; return true; } public Set getHeaders() { return null; } public boolean handleFault(SOAPMessageContext context) { return true; } public void close(MessageContext context) { } }
import java.util.ArrayList; import java.util.List; import javax.xml.ws.handler.HandlerResolver; import javax.xml.ws.handler.PortInfo; public class MyHandlerResolver implements HandlerResolver { public List getHandlerChain(PortInfo portInfo) { ArrayListTo associate your handler with the web service, you need to do something like this:handlerChain = new ArrayList (); handlerChain.add(new MyHandler()); return handlerChain; } }
myws.service = new myws_Service(); System.out.println("Now setting the HandlerResolver"); // Following two HandlerResolver myHanlderResolver = new MyHandlerResolver(); service.setHandlerResolver(myHanlderResolver); System.out.println("myHandlerResolver has been set.");
How to convert SOAPMessage to a String
'
private String log(SOAPMessage message) throws SOAPException, IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); message.writeTo(out); logger.debug(out.toString()); return out.toString(); }You might like reading this : http://javakafunda.blogspot.in/2012/04/how-to-format-xml-string-in-java.html
thanks for this article
ReplyDelete1) The service.setHandlerResolver() thing did nothing on the client side, instead the add handler that you mention on the server side worked.
ReplyDelete2) You might want to correct the typoes, like soapmessagecontext and myhandler. And the ArrayList must be of type Handler, not myhandler.
3) After doing all this it worked, so thank you nevertheless.
it worked for me. thanks a lot
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI followed your instruction, but some reason it does not execute MyHandler class. I am using Websphere application server and used RAD to generated client stub (Generated By:JAX-WS RI IBM 2.1.6 in JDK 6 (JAXB RI IBM JAXB 2.1.10 in JDK 6). Please advise.
ReplyDeleteThis thing worked for me n websphere and is currently running in production. Visit source link, you might find something useful.
DeleteThis thing worked for me n websphere and is currently running in production. Visit source link, you might find something useful.
DeleteI created handler-chain.xml and used @HandlerChain in my Service class for it to work. However, my next question is can I use the same SoapHandler implemented class to process different webservices.
DeleteThank you.
No. I think this can be used to process only 1 webservice as each webservice needs to have its own handler.
DeleteI need a java sample application to sign a SOAP request with a public key. Can someone help?
ReplyDeleteHere I leave a way to save the logs of soap messages with c#
ReplyDeletehttp://www.systemdeveloper.org/2014/06/tracesoapmessage
This comment has been removed by the author.
ReplyDeleteCool and I have a super offer: What Do House Renovations Cost split level home additions
ReplyDelete