How to get payload object from SOAP Logical Handle

2019-02-15 18:10发布

问题:

I am learning how to create Logical Handlers in JAX-WS SOAP web services. Here I am trying to get the payload data and want to print that for testing. But I am getting issues.

Here is my LogincalHandler code

public class HelloMessengerLogicalHandler implements LogicalHandler<LogicalMessageContext> {

    public void close(MessageContext ctx) {
    }

    public boolean handleFault(LogicalMessageContext ctx) {
        return false;
    }

    public boolean handleMessage(LogicalMessageContext ctx) {
        Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if(outbound) {
            LogicalMessage message = ctx.getMessage();
            //Source payload = message.getPayload();
            JAXBContext jaxbContext;
            try {
                jaxbContext = JAXBContext.newInstance(Person.class);
                Object jaxbPayload = message.getPayload(jaxbContext);
                System.out.println(jaxbPayload);
            } catch (JAXBException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        return true;
    }
}

Here is my webservice code:

@WebService
@HandlerChain(file = "handler-chain.xml")
public class HelloMessenger {
    public Person getPerson(String name) {
        return new Person(name);
    }
}

This is my Person class:

public class Person {

    // Default Constructor & Getters, Setters
    private String name;

    public Person(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + "]";
    }
}

When I publish my code and access the webservice I am getting below exception:

javax.xml.ws.WebServiceException: javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXParseException2; unexpected element (uri:"http://simple/", local:"getPersonResponse"). Expected elements are (none)]
    at com.sun.xml.internal.ws.handler.LogicalMessageImpl.getPayload(LogicalMessageImpl.java:121)
    at simple.HelloMessengerLogicalHandler.handleMessage(HelloMessengerLogicalHandler.java:31)
    at simple.HelloMessengerLogicalHandler.handleMessage(HelloMessengerLogicalHandler.java:1)
    at com.sun.xml.internal.ws.handler.HandlerProcessor.callHandleMessageReverse(HandlerProcessor.java:326)
    at com.sun.xml.internal.ws.handler.HandlerProcessor.callHandlersResponse(HandlerProcessor.java:197)
    at com.sun.xml.internal.ws.handler.ServerLogicalHandlerTube.callHandlersOnResponse(ServerLogicalHandlerTube.java:158)
    at com.sun.xml.internal.ws.handler.HandlerTube.processResponse(HandlerTube.java:149)
    at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:636)
    at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:585)
    at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:570)
    at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:467)
    at com.sun.xml.internal.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:299)
    at com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:593)
    at com.sun.xml.internal.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:244)
    at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handleExchange(WSHttpHandler.java:95)
    at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handle(WSHttpHandler.java:80)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
    at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:80)
    at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:677)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
    at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:649)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

I am a newbie, my understanding is the payload object is Person, so it should display the person object in logs, but I am seeing this error.

Please explain what does this error indicates & how to get the payload data in logical handler?

回答1:

Source payload = message.getPayload();

then convert the DomSource object to string by using xmltransformation or some other way , it will give you the payload.



回答2:

As @curious said, use

Source payload = message.getPayload();

Sample code for convert the DomSource object to string by using xmltransformation is like this :

StringWriter sw = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult( sw );
transformer.transform( payload, output );
System.out.println("Response : "+sw.toString());