如何从SOAP逻辑处理程序

时间:2016-04-25 21:58:19

标签: java web-services soap jax-ws

我正在学习如何在JAX-WS SOAP Web服务中创建逻辑处理程序。在这里,我试图获取有效载荷数据,并希望打印出来进行测试。但是我遇到了问题。

这是我的LogincalHandler代码

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;
    }
}

这是我的网络服务代码:

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

这是我的Person课程:

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 + "]";
    }
}

当我发布我的代码并访问网络服务时,我遇到异常:

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)

我是新手,我的理解是有效负载对象是Person,所以它应该在日志中显示person对象,但我看到了这个错误。

请解释此错误表示的内容&amp;如何在逻辑处理程序中获取有效负载数据?

2 个答案:

答案 0 :(得分:2)

Source payload = message.getPayload();

然后使用xmltransformation或其他方式将DomSource对象转换为字符串,它将为您提供有效负载。

答案 1 :(得分:0)

就像@ {curious说的那样,使用

Source payload = message.getPayload();

使用xmltransformation将DomSource对象转换为字符串的示例代码如下:

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