解析SOAPMessage响应变为null

时间:2016-06-15 09:03:27

标签: java web-services soap xml-parsing unmarshalling

我从soap webservice获取xml响应但是当我解组它时我得到null。当我在控制台上打印它时它打印正确但是当我解组时它打印为空。

以下是获得回复后解组的代码

private static void printSOAPResponse(SOAPMessage soapResponse)
   {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
        XMLInputFactory xif = XMLInputFactory.newFactory();
       //StreamSource xml = new StreamSource(soapResponse.toString());
        XMLStreamReader xsr = xif.createXMLStreamReader(sourceContent);
        xsr.nextTag();
        while(!xsr.getLocalName().equals("HelpDesk_Query_ServiceResponse")) {
          xsr.nextTag();
        }

    JAXBContext jc = JAXBContext.newInstance(HelpDesk_Query_ServiceResponse.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    JAXBElement<HelpDesk_Query_ServiceResponse> jb = unmarshaller.unmarshal(xsr, HelpDesk_Query_ServiceResponse.class);
    xsr.close();
    HelpDesk_Query_ServiceResponse response = jb.getValue();

    System.out.println(response.City);
    System.out.println(response.Organization);
}

下面是我的java pojo类

@XmlAccessorType(XmlAccessType.FIELD)
public class HelpDesk_Query_ServiceResponse {
    @XmlAttribute
    String Assigned_Group;
    @XmlAttribute 
    String Organization;
    @XmlAttribute 
    String City;
//other attribute and their Getter and setter
}

这一行 transformer.transform(sourceContent,result); 正在这样的控制台中打印响应

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
    <ns0:HelpDesk_Query_ServiceResponse xmlns:ns0="urn:XXXX_HPD_IncidentInterface_WS__XXXXX" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <ns0:Assigned_Group>TIM-CSDWINDOWS-ADMIN</ns0:Assigned_Group>
        <ns0:Assigned_Group_Shift_Name/>
        <ns0:City>HYDERABAD</ns0:City>
        <ns0:Organization>XXX_TIM</ns0:Organization>
        <ns0:Priority>Medium</ns0:Priority>
        <ns0:Product_Model_Version/>
        <ns0:Product_Name/>
        <ns0:HPD_CI_ReconID/>
        <ns0:z1D_CI_FormName/>
    </ns0:HelpDesk_Query_ServiceResponse>
</soapenv:Body>

但是当我解组并尝试打印城市和组织价值时,它会打印为空。 请有人帮助我。

1 个答案:

答案 0 :(得分:0)

您有两个问题,命名空间和使用属性而不是元素:

基本上,你应该使用:

<camelContext xmlns="http://camel.apache.org/schema/spring">
        <route >
            <from id ="server" uri="jetty:http://0.0.0.0:9500/rsb/toService?matchOnUriPrefix=true&amp;enableMultipartFilter=false&amp;disableStreamCache=false"/>
            <wireTap uri="log:test?level=INFO"><body><simple>Enter JMS test route and sending message to queue</simple></body></wireTap>
            <!--<to uri="direct:queue"/>-->
            <to uri="jms://testqueue?requestTimeout=360000&amp;replyTo=bar&amp;replyToDeliveryPersistent=true&amp;exchangePattern=InOut&amp;acknowledgementModeName=AUTO_ACKNOWLEDGE"/>
        </route>
        <route id="testqueuelistener" streamCache="true">
            <from uri="jms://testqueue?replyToDeliveryPersistent=true&amp;replyTo=bar" />
            <wireTap uri="log:test?level=INFO"><body><simple>Message recieved from queue: ${body}</simple></body></wireTap>             
            <to uri="http://localhost:18402/Home/addUser?bridgeEndpoint=true&amp;throwExceptionOnFailure=false"/>
            <to uri="jms:service"/> 
        </route>
        <route >
            <from uri="jms:service"/>
            <transform>
                <simple>${body}</simple>
            </transform>
        </route>
    </camelContext>