how get xml responce using JAX-WS SOAP handler

Posted by khris on Stack Overflow See other posts from Stack Overflow or by khris
Published on 2013-10-17T15:51:44Z Indexed on 2013/10/17 15:54 UTC
Read the original article Hit count: 235

Filed under:
|
|

I have implemented web service:

@WebServiceClient(//parameters//)
@HandlerChain(file = "handlers.xml")
public class MyWebServiceImpl {...}

Also I have implemented ObjectFactory with list of classes for creating my requests and responses. For Example class Test.

I need to get xml of response.

I try to use JAX-WS SOAP handler, so I add this @HandlerChain(file = "handlers.xml") anotation. My handlers.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
<handler-chain>
<handler>
<handler-class>java.com.db.crds.ws.service.LoggingHandler</handler-class>
</handler>
</handler-chain>
</handler-chains>

My LoggingHandler class is:

import java.io.PrintWriter;
import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPMessageContext;

public class LoggingHandler implements javax.xml.ws.handler.soap.SOAPHandler<SOAPMessageContext>     {

    public void close(MessageContext messagecontext) {
    }

    public Set<QName> getHeaders() {
        return null;
    }

    public boolean handleFault(SOAPMessageContext messagecontext) {
        return true;
    }

    public boolean handleMessage(SOAPMessageContext smc) {
        Boolean outboundProperty = (Boolean) smc.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        if (outboundProperty.booleanValue()) {
            System.out.println("\nOutbound message:");
        } else {
        System.out.println("\nInbound message:");
        }

        SOAPMessage message = smc.getMessage();

        try {
        PrintWriter writer = new PrintWriter("soap_responce" + System.currentTimeMillis(), "UTF-8");
        writer.println(message);
        writer.close();
        message.writeTo(System.out);
        System.out.println("");   // just to add a newline
    } catch (Exception e) {
        System.out.println("Exception in handler: " + e);
    }
    return outboundProperty;   
}

}

I have test class which creates request, here are part of code:

MyWebServiceImpl impl = new MyWebServiceImpl(url, qName);
    ws = impl.getMyWebServicePort();
Test req = new Test();

I suppose to get xml response in file "soap_responce" + System.currentTimeMillis(). But such file isn't even created. Please suggest how to get xml response, I'm new to web services and may do something wrong. Thanks

© Stack Overflow or respective owner

Related posts about soap

Related posts about jax-ws