I have a response from a 3rd party API, that give me an error for unmarshalling the xml response is similar to that:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:webServiceResponse
xmlns="http://ws.services.company.com"
xmlns:ns2="http://rest.ws.services.company.com">
<ns2:result>
<success>true</success>
<id>7054986</id>
</ns2:result>
</ns2:webServiceResponse>
Normally I was testing my Java code with a xml response like that and I get success:
<webServiceResponse xmlns="http://ws.services.company.com">
<result>
<success>true</success>
<id>1</id>
</result>
</webServiceResponse>
In Java, I have been creating the respective XML Data class with the following xsd files.
CompanyWS.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema elementFormDefault="qualified" xmlns:jxb="https://jakarta.ee/xml/ns/jaxb" jxb:version="3.0" targetNamespace="http://ws.services.company.com" xmlns="http://ws.services.company.com" version="1.0" xmlns:ns2="http://ws.services.company.com" xmlns:ws="http://ws.services.company.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="CompanyWSTypes.xsd"/>
<xsd:element name="someFunctionality">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="ns2:WebServiceRequest">
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="someEvent" type="ns2:InteractObject"/>
<xsd:element maxOccurs="1" minOccurs="1" name="recipientData" type="ns2:RecipientData"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="someFunctionality2">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="ns2:WebServiceRequest">
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="customEvent" type="ns2:CustomEvent"/>
<xsd:element maxOccurs="1" minOccurs="1" name="recipientData" type="ns2:RecipientData"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="webServiceResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="result" nillable="true" type="ns2:WebServiceResult"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
CompanyWSTypes.xsd
<?xml version="1.0" encoding="UTF-8"?><xsd:schema elementFormDefault="qualified" xmlns:jxb="https://jakarta.ee/xml/ns/jaxb" jxb:version="3.0" targetNamespace="http://ws.services.company.com" version="1.0" xmlns:ns2="http://ws.services.company.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="InteractObject">
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="folderName" type="ns2:minLengthString"/>
<xsd:element maxOccurs="1" minOccurs="1" name="objectName" type="ns2:minLengthString"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="RecipientData">
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="recipient" type="ns2:Recipient"/>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="optionalData" nillable="true" type="ns2:OptionalData"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Recipient">
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="listName" type="ns2:InteractObject"/>
<xsd:element maxOccurs="1" minOccurs="0" name="recipientId" nillable="true" type="xsd:long"/>
<xsd:element maxOccurs="1" minOccurs="0" name="customerId" nillable="true" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="mobileNumber" nillable="true" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="emailAddress" nillable="true" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="permissionStatus" nillable="true" type="ns2:PermissionStatus"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="OptionalData">
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="name" type="ns2:minLengthString"/>
<xsd:element maxOccurs="1" minOccurs="1" name="value" type="ns2:minLengthString"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="minLengthString">
<xsd:restriction base="xsd:string">
<xsd:minLength value="1"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="CampaignType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="TRANSACTIONAL"/>
<xsd:enumeration value="PROMOTIONAL"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="PermissionStatus">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="OPTIN"/>
<xsd:enumeration value="OPTOUT"/>
<xsd:enumeration value="NOT_SPECIFIED"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="PriorityLevel">
<xsd:restriction base="xsd:int">
<xsd:enumeration value="1"/>
<xsd:enumeration value="2"/>
<xsd:enumeration value="3"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="WebServiceRequest">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0" name="additionalData" nillable="true" type="xsd:anyType"/>
</xsd:sequence>
<xsd:attribute name="transactionId" type="xsd:string"/>
<xsd:attribute name="accountId" type="xsd:string"/>
<xsd:attribute name="brandId" type="xsd:string"/>
<xsd:attribute name="langLocale" type="xsd:string"/>
<xsd:attribute default="1" name="priorityLevel" type="ns2:PriorityLevel"/>
<xsd:attribute default="TRANSACTIONAL" name="campaignType" type="ns2:CampaignType"/>
</xsd:complexType>
<xsd:complexType name="CustomEvent">
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0" name="eventName" nillable="true" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="eventId" nillable="true" type="xsd:long"/>
<xsd:element maxOccurs="1" minOccurs="0" name="eventStringDataMapping" nillable="true" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="eventDateDataMapping" nillable="true" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="eventNumberDataMapping" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="WebServiceResult">
<xsd:sequence>
<xsd:element name="success" type="xsd:boolean"/>
<xsd:element minOccurs="0" name="id" nillable="true" type="xsd:long"/>
<xsd:element minOccurs="0" name="error" nillable="true" type="ns2:WebServiceError"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="WebServiceError">
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0" name="code" nillable="true" type="ns2:ErrorCode"/>
<xsd:element maxOccurs="1" minOccurs="0" name="message" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="ErrorCode">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="MISSING_REQUIRED_FIELD"/>
<xsd:enumeration value="DATABASE_ERROR"/>
<xsd:enumeration value="SERVER_ERROR"/>
<xsd:enumeration value="LOGIN_ERROR"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
The error I get:
Unable to unmarshal to WebServiceResponse.
jakarta.xml.bind.UnmarshalException: unexpected element (uri:"http://rest.ws.services.company.com", local:"webServiceResponse"). Expected elements are <{http://ws.services.company.com}someFunctionality>,<{http://ws.services.company.com}someFunctionality2>,<{http://ws.services.company.com}webServiceResponse>
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:693) ~[task/:?]
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.Loader.reportError(Loader.java:230) ~[task/:?]
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.Loader.reportError(Loader.java:225) ~[task/:?]
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:92) ~[task/:?]
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1102) ~[task/:?]
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:527) ~[task/:?]
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:509) ~[task/:?]
at org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:137) ~[task/:?]
.
.
.
Can anyone help regarding this issue?
I think it cant associate the response namespace with ns2 which is associated with the uri start as rest. But I cant to do it on xsd files to associate with ns2 as it gives error all time.
Your target namespace in XSD, and so in your Java generated code is
targetNamespace="http://ws.services.company.com"but you receive thewebServiceResponsein thehttp://rest.ws.services.company.comnamespace.That why the unmarshaller complains about not receiving one of the expected elements
Is your XSD created by yourself or send by someone else ? In any case to make it work, you should change the
targetNamespaceaccording to the expected one (with therestprefix in it).