我设置了eBay通知,我想将通知内容(即SOAP XML)转换为GetItemTransactionsResponse Java Object。以下是我正在使用的代码。
SOAPMessage message = MessageFactory.newInstance().createMessage(null,
new ByteArrayInputStream(
CONVERT_EBAY_NOTIFICATION_TO_BYTE_ARRAY_METHOD()
));
SOAPEnvelope soapEnv = message.getSOAPPart().getEnvelope();
SOAPBody soapBody = message.getSOAPBody();
JAXBContext content = JAXBContext.newInstance(GetItemTransactionsResponseType.class);
Unmarshaller unmarshaller = content.createUnmarshaller();
JAXBIntrospector jaxbIntrospector = content.createJAXBIntrospector();
Object transactionObject = unmarshaller.unmarshal(soapBody, GetItemTransactionsResponseType.class);
GetItemTransactionsResponseType transaction = (GetItemTransactionsResponseType)jaxbIntrospector.getValue(transactionObject);
我认为这会将SOAP XML转换为相应的eBay对象,但它并不起作用,因为"事务" object为null ...我试图访问" transaction"中的变量。对象
transaction.getItem()
我收到NullPointerException ...
然后我检查我的soapBody对象,看看我是否传入导致NullPointerException的空内容
soapBody.getTextContent()
我可以在soapBody对象中看到文本内容。它不是空的!
我真的很感激能解决这个问题的任何帮助。不确定我是否错误地转换了SOAP XML。提前谢谢!
答案 0 :(得分:0)
不知怎的,我设法找出了问题。
我实际上需要使用Document:
获取SOAP XML的正文部分Document document = soapBody.extractContentAsDocument();
然后我将Document对象而不是SOAPBody对象解组为Object:
Object transactionObject = unmarshaller.unmarshal(document);
最后,我使用JAXBIntrospector获取对象值,并将对象转换为GetItemTransactionsResponseType类,就像我最初所做的那样:
GetItemTransactionsResponseType transaction = (GetItemTransactionsResponseType)jaxbIntrospector.getValue(transactionObject);
我将把代码留在这里,希望能帮助那些像我一样挣扎的人。
SOAPMessage message = MessageFactory.newInstance().createMessage(null,
new ByteArrayInputStream(
REPLACE_THIS_WITH_YOUR_IMPLEMENTATION_TO_GET_YOUR_SOURCE()
));
SOAPBody soapBody = message.getSOAPBody();
Document document = soapBody.extractContentAsDocument();
JAXBContext content = JAXBContext.newInstance(GetItemTransactionsResponseType.class);
Unmarshaller unmarshaller = content.createUnmarshaller();
JAXBIntrospector jaxbIntrospector = content.createJAXBIntrospector();
Object transactionObject = unmarshaller.unmarshal(document);
GetItemTransactionsResponseType transaction = (GetItemTransactionsResponseType)jaxbIntrospector.getValue(transactionObject);