khris
khris

Reputation: 4979

how get xml responce using JAX-WS SOAP handler

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.webservice.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

Upvotes: 0

Views: 4456

Answers (1)

Dawid Pytel
Dawid Pytel

Reputation: 2810

Using SOAP handlers is IMHO perfectly fine for such a task. I would approach it the same way.

I was able to use your configuration with minor modifications to get the example running. As a result I am able to see generated files. If you can't see them please check whether you check correct path, e.g. using:

File file = new File("soap_responce" + System.currentTimeMillis());
System.out.println(file.getAbsolutePath());

What I changed is:

  • package from java.com.webservice.service.LoggingHandler to com.webservice.service.LoggingHandler as packages starting with java are forbidden

Complete project can be found here: https://github.com/destin/SO-answers/tree/master/SO-how-get-xml-responce-using-jax-ws-soap-handler

org.dpytel.jaxws.jaxws_java_first_jboss.client.Main class shows how I get and execute the web service.

BTW. you don't need to implement client stub and object factory etc when you have WSDL file. You can use wsimport tool. You can check how to use it in mentioned project.

Upvotes: 1

Related Questions