提问人:methamphetamine 提问时间:11/12/2023 最后编辑:methamphetamine 更新时间:11/13/2023 访问量:21
当我尝试取消编组 WebServiceResponse 时出现解组错误
Unmarshalling Error when I try to unmarshall the WebServiceResponse
问:
我有一个来自第三方 API 的响应,它给了我一个解组 xml 响应的错误,类似于:
<?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>
通常,我使用这样的 xml 响应测试我的 Java 代码,并且我取得了成功:
<webServiceResponse xmlns="http://ws.services.company.com">
<result>
<success>true</success>
<id>1</id>
</result>
</webServiceResponse>
在 Java 中,我一直在使用以下 xsd 文件创建相应的 XML Data 类。
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>
我得到的错误:
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/:?]
.
.
.
谁能帮忙解决这个问题?
我认为它不能将响应命名空间与 ns2 相关联,ns2 与 uri start 相关联。但是我无法在 xsd 文件上执行此操作以与 ns2 相关联,因为它一直存在错误。rest
答:
0赞
Laurent Schoelens
12/2/2023
#1
XSD 中的目标命名空间,以及 Java 生成的代码中的目标命名空间,但您在命名空间中收到。targetNamespace="http://ws.services.company.com"
webServiceResponse
http://rest.ws.services.company.com
这就是为什么解组员抱怨没有收到预期的元素之一的原因
您的 XSD 是由您自己创建的还是由其他人发送的?无论如何,要使它工作,您应该根据预期的(带有前缀)进行更改。targetNamespace
rest
评论