gpt4 book ai didi

java - SOAPMessage.getMessageBody() 打印 null

转载 作者:行者123 更新时间:2023-12-02 12:27:30 27 4
gpt4 key购买 nike

各位,我不明白如果message.getSOAPBody()打印为null,那么为什么unmarshaller.unmarshal(message.getSOAPBody(), Person.class);返回一个值给变量 jb。请帮助我理解。

String example="<SOAP:Envelope 
xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<SOAP:Body>"
+ "<firstName>Jane</firstName>"
+ "<lastName>Doe</lastName>"
+ "</SOAP:Body>"
+ "</SOAP:Envelope>";

ByteArrayInputStream bas=new ByteArrayInputStream(example.getBytes());

try {
SOAPMessage message = MessageFactory.newInstance().createMessage(null,bas);
System.out.println(message.getSOAPBody().toString());
JAXBContext jc = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();

System.out.println(message.getSOAPBody()); // this prints null
System.out.println(message.getSOAPBody().toString()); // this prints null
JAXBElement<Person> jb = unmarshaller.unmarshal(message.getSOAPBody(), Person.class); // I am not able to undestand if above lines return null, then how come this line populates jb

Person p = jb.getValue();
System.out.println(p.firstName); // prints Jane
System.out.println(p.lastName); // prints Doe
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SOAPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

这是程序的输出:

[SOAP:Body: null]
[SOAP:Body: null]
[SOAP:Body: null]
Jane
Doe

You are checking wrong method, you should try changing message.getSOAPBody().toString() to message.getSOAPBody().getTextContent() or getChildNodes() etc to validate the message content.

以下是 message.getSOAPBody().getTextContent() 的输出

[SOAP:Body: null]
[SOAP:Body: null]
JaneDoe
Jane
Doe

关于java - SOAPMessage.getMessageBody() 打印 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45431195/

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