gpt4 book ai didi

java - 如何从 SOAP 逻辑处理程序获取有效负载对象

转载 作者:行者123 更新时间:2023-11-30 06:09:57 26 4
gpt4 key购买 nike

我正在学习如何在 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 对象,但我看到了这个错误。

请解释此错误指示什么以及如何在逻辑处理程序中获取有效负载数据?

最佳答案

源payload = message.getPayload();

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

关于java - 如何从 SOAP 逻辑处理程序获取有效负载对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36851843/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com