各位,我不明白如果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/