1

I am consuming a web-service which has the soap message header as follows.

<SOAP-ENV:Header>
      <MessageHeader xmlns:cnr="http://testservice.com/Namespaces/Types/Public/DataModel.xsd" xmlns="http://testservice.com/Namespaces/Types/Public/MessageHeader.xsd">
         <CommonMessageHeader>
            <cnr:version>v109</cnr:version>
            <cnr:dateTimeStamp>2016-08-31T14:37:03Z</cnr:dateTimeStamp>
            <cnr:referenceId>cc7b-429c-a2f5-1f61dcb94e85</cnr:referenceId>
         </CommonMessageHeader>
         <SecurityMessageHeader>
            <cnr:userName>dtv_directvcom</cnr:userName>
            <cnr:userPassword>dtv_directvcom0624</cnr:userPassword>
         </SecurityMessageHeader>        
      </MessageHeader>
   </SOAP-ENV:Header>

I am trying to convert this soap header into java object after getting response but facing below error.I am getting the response properly (compared with SoapUI).

unexpected element (uri:"http://testservice.com/Namespaces/Profile/Types/Public/MessageHeader.xsd", local:"MessageHeader").
Expected elements are <{http://testservice.com/Namespaces/Types/Public/SoapFaultDetails.xsd}ApplicationException>,
<{http://testservice.com/Namespaces/Container/Public/ServiceRequest.xsd}ServiceRequest>,
<{http://testservice.com/Namespaces/Container/Public/ServiceResponse.xsd}ServiceResponse>,
<{http://testservice.com/Namespaces/Types/Public/MessageHeader.xsd}MessageHeader>

Below is the code snippet used.

NodeList nodeList = soapMessage.getSOAPHeader().getElementsByTagName("MessageHeader");
if ( (nodeList != null) && (nodeList.getLength() > 0) ) {
       Node header = nodeList.item(0);
       JAXBElement jaxbElement = null;
       Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
       jaxbElement = (JAXBElement) unmarshaller.unmarshal(header);
       if (jaxbElement != null) {
            Object value = jaxbElement.getValue();
            if ( (value != null) && (value instanceof JavaObject) ) {
                    setResponseHeader((JavaObject) value);
                }
            }
        }

I am not sure how the MessageHeader of another service is coming as namespace URI. Can anyone help me on this please?

Vijay
  • 19
  • 1
  • 5
  • I have passed the application name of another service in message header. This was the issue. Resolved it now by passing correct value. Thanks everyone for your help. – Vijay Oct 25 '16 at 07:08

1 Answers1

1

Something like this maybe can help you:

JAXBContext jaxbContext = JAXBContext.newInstance(YourClass.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

JAXBElement<YourClass> root = jaxbUnmarshaller.unmarshal(new StreamSource(
        header), YourClass.class);
YourClass response = root.getValue();

You can read the full documentation here. Where you can find your error also. It is very usefull.

Lazar Lazarov
  • 2,412
  • 4
  • 26
  • 35