Can not unmarshall the SOAP response

前端 未结 1 727
天命终不由人
天命终不由人 2020-12-18 11:36

I could send a request and receive the response but I can not parse the response. It returns the following error:

Local Name:Body
error is here
java.lang.Nu         


        
1条回答
  •  春和景丽
    2020-12-18 12:26

    Ensure You Are Unmarshalling the Correct Thing

    You need to unmarshal the content held onto by SOAP body, not the entire SOAP message. Below is what the code might look like:

    SOAPMessage sm = response;
    SOAPBody sb = response.getSOAPBody();
    Document d = sb.extractContentAsDocument();
    DOMSource source = new DOMSource(d);
    results = (FindEventsResponse) JAXB.unmarshal(source, FindEventsResponse.class);
    

    The result of sb.extractContentAsDocument is going to be a DOM that is equivalent to the following:

    
    
        
            
    20662 414 1 50
    1516682 18004C6E8D7218A8 onSale The Art of the Brick http://www.ticketmaster.ie/event/18004C6E8D7218A8?camefrom=CFC_UK_BUYAT&brand=[=BRAND=] 2014-05-23 10:00:00 0000-00-00 00:00:00 0000-00-00 00:00:00 Exhibitions 754 Family & Attractions 10003 17 17 1806028 1663495 The Art of the Brick http://www.ticketmaster.co.uk/The-Art-of-the-Brick-tickets/artist/1663495?camefrom=CFC_UK_BUYAT&brand=[=BRAND=] http://media.ticketmaster.com/tm/en-us/tmimages/TM_GenCatImgs_Generic_BW.jpg Miscellaneous 0 Miscellaneous 10005 3331 198292 Ambassador Theatre Oconnell Street Dublin United Kingdom Dublin 1 http://www.ticketmaster.ie/Ambassador-Theatre-tickets-Dublin/venue/198292?camefrom=CFC_UK_BUYAT&brand= http://media.ticketmaster.co.uk/tmimages/TM_GenVenueImg_BW.jpg

    UPDATE

    Based on the new exception you are getting:

    java.lang.IllegalArgumentException: prefix xsd is not bound to a namespace
        at com.sun.xml.internal.bind.DatatypeConverterImpl._parseQName(DatatypeConverterImpl.java:346)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.LeafPropertyXsiLoader.selectLoader(LeafPropertyXsiLoader.java:75)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.LeafPropertyXsiLoader.startElement(LeafPropertyXsiLoader.java:58)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:486)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
    

    It appears as though sb.extractContentAsDocument(); is not bringing along the xmlns:xsd declaration from the SOAP message. Instead you can change the code to do the following:

    SOAPMessage sm = response;
    SOAPBody sb = response.getSOAPBody();
    DOMSource source = new DOMSource(sb.getFirstChild());
    results = (FindEventsResponse) JAXB.unmarshal(source, FindEventsResponse.class);
    

    Ensure the Namespace Qualification is Mapped Correctly

    Having the following tells JAXB that everything mapped to an XML element without an explicitly specified namespace should belong in the http://ticketmaster.productserve.com/v2/soap.php namespace.

    @XmlSchema( 
        namespace = "http://ticketmaster.productserve.com/v2/soap.php",
        elementFormDefault = XmlNsForm.QUALIFIED) 
    package com.ticketmaster.ticketmaster;
    import javax.xml.bind.annotation.XmlNsForm;
    import javax.xml.bind.annotation.XmlSchema;
    

    In your XML only the findEventsResponse element is namespace qualified. This means your annotation should be the following instead (note change from QUALIFIED to UNQUALIFIED):

    @XmlSchema( 
        namespace = "http://ticketmaster.productserve.com/v2/soap.php",
        elementFormDefault = XmlNsForm.UNQUALIFIED) 
    package com.ticketmaster.ticketmaster;
    import javax.xml.bind.annotation.XmlNsForm;
    import javax.xml.bind.annotation.XmlSchema;
    

    Demo Code

    Using the above XML as input.xml I was able to unmarshal and marshal the model as you have defined it in your question with the @XmlSchema fix mentioned above.

    import javax.xml.bind.*;
    import javax.xml.transform.stream.StreamSource;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(FindEventsResponse.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            StreamSource xml = new StreamSource("src/forum23806625/input.xml");
            JAXBElement response = unmarshaller.unmarshal(xml, FindEventsResponse.class);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(response, System.out);
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题