我在 JAXB 将 xml 文件解码到 Request 对象并将其发送到服务时遇到了一些麻烦。
它返回错误而不是正确的响应 - 未知用户
。
我正在使用下一个架构:
xml file with test date => parsing to RQ object => sent to servis => take response and check it.
这是源 xml 文件:
<OTA_AirLowFareSearchRQ EchoToken="50987" SequenceNmbr="1" Target="Production" TimeStamp="2003-11-19T19:44:10-05:00"
Version="2.001"
xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05 OTA_AirLowFareSearchRQ.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.opentravel.org/OTA/2003/05">
<POS>
<TPA_Extensions>
<TPA_Extension>
<PromoRatesRequired Value="false"/>
<UserName Value="342561"/>
<UserPassword Value="1234"/>
<ClearCache Value="true"/>
</TPA_Extension>
</TPA_Extensions>
</POS>
<OriginDestinationInformation>
<DepartureDateTime>2015-04-13T00:00:00</DepartureDateTime>
<OriginLocation LocationCode="DUB"/>
<DestinationLocation LocationCode="CDG"/>
</OriginDestinationInformation>
<TravelPreferences>
<CabinPref PreferLevel="Preferred" Cabin="Economy"/>
</TravelPreferences>
<TravelerInfoSummary>
<AirTravelerAvail>
<PassengerTypeQuantity Code="ADT" Quantity="1"/>
<PassengerTypeQuantity Code="CHD" Quantity="0"/>
<PassengerTypeQuantity Code="INF" Quantity="1"/>
</AirTravelerAvail>
</TravelerInfoSummary>
</OTA_AirLowFareSearchRQ>
我正在使用 JAXB 解析并转换为我的 RQ
对象。
它返回奇怪的错误而不是响应。
我只是将未解析的请求写入 xml 文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:OTA_AirLowFareSearchRQ xmlns:ns1="http://www.opentravel.org/OTA/2003/05" Version="2.001" EchoToken="50987"
SequenceNmbr="1" TimeStamp="2003-11-20T00:44:10.000Z" Target="Production"
DirectFlightsOnly="false">
<ns1:POS>
<ns1:TPA_Extensions>
<ns1:TPA_Extension>
<ns1:PromoRatesRequired Value="false"/>
<ns1:UserName Value="342561"/>
<ns1:UserPassword Value="1234"/>
<ns1:ClearCache Value="true"/>
</ns1:TPA_Extension>
</ns1:TPA_Extensions>
</ns1:POS>
<ns1:OriginDestinationInformation>
<ns1:DepartureDateTime>2015-04-13T00:00:00</ns1:DepartureDateTime>
<ns1:OriginLocation LocationCode="DUB" CodeContext="IATA"/>
<ns1:DestinationLocation LocationCode="CDG" CodeContext="IATA"/>
</ns1:OriginDestinationInformation>
<ns1:TravelPreferences OriginDestinationRPHs="" ETicketDesired="false" SmokingAllowed="false">
<ns1:CabinPref PreferLevel="Preferred" Cabin="Economy"/>
</ns1:TravelPreferences>
<ns1:TravelerInfoSummary>
<ns1:AirTravelerAvail>
<ns1:AirTravelerAvail>
<ns1:PassengerTypeQuantity Code="ADT" Quantity="1"/>
<ns1:PassengerTypeQuantity Code="CHD" Quantity="0"/>
<ns1:PassengerTypeQuantity Code="INF" Quantity="1"/>
</ns1:AirTravelerAvail>
</ns1:AirTravelerAvail>
</ns1:TravelerInfoSummary>
<ns1:dataStatus>SUCCESS_LOW_FARE_SEARCH_REQUEST</ns1:dataStatus>
</ns1:OTA_AirLowFareSearchRQ>
它有很多讨厌的 ns1:
名称前缀。
我想知道如何在没有这些冗余前缀的情况下进行解码?
最佳答案
您可以使用以下包级别 @XmlSchema
注释在 XML 文档中正确映射命名空间限定。包级别注释放在一个名为 package-info
的特殊类中。
package.info.java
下面是 package-info
类的完整源代码。
@XmlSchema(
namespace = "http://www.opentravel.org/OTA/2003/05",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
有关 JAXB 和命名空间的更多信息
以下是我博客上的帖子的链接,您可能会觉得有用:
关于java - 如何使用 JAXB 解码自定义 xml 命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26179622/