【问题标题】:JAXB unmarshalling returns 'URI is not absolute' errorJAXB 解组返回“URI 不是绝对的”错误
【发布时间】:2014-07-02 09:36:45
【问题描述】:

我收到了来自 webservice 的以下响应,并且正在使用以下代码来解组​​响应,但它返回以下错误。

SEVERE:   java.lang.IllegalArgumentException: URI is not absolute

回应

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org  
                  /2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <SearchFlightsResponse xmlns="ElsyArres.API">
            <SoapMessage>
                <Username>TEST</Username>
                <Password>TESTs</Password>
                <LanguageCode>EN</LanguageCode>
                <ErrorMessage />
                <ErrorCode>0</ErrorCode>
                <AppVersion>8.0.3</AppVersion>
                <Request>
                    <Departure>FDH</Departure>
                    <Destination>HAM</Destination>
                    <DepartureDate>2014-08-08</DepartureDate>
                    <ReturnDate />
                    <NumADT>1</NumADT>
                    <NumINF>0</NumINF>
                    <NumCHD>0</NumCHD>
                    <CurrencyCode>EUR</CurrencyCode>
                    <CarrierList />
                    <FareClasses />
                    <Providers />
                    <WaitForResult>true</WaitForResult>
                    <NearbyDepartures>true</NearbyDepartures>
                    <NearbyDestinations>true</NearbyDestinations>
                    <RROnly>false</RROnly>
                    <MetaSearch>true</MetaSearch>
                </Request>
                <Response>
                    <SearchFlightId>140514114543-02-43064-52</SearchFlightId>
                    <Roundtrip>false</Roundtrip>
                    <CurrencyCode>EUR</CurrencyCode>
                    <Flights>
                        <Flight>
                            <Outbound>
                                <CarName>Inter Sky</CarName>
                                <CarCode>3L</CarCode>
                                <DepName>Friedrichshafen</DepName>
                                <DepCode>FDH</DepCode>
                                <DestName>Hamburg</DestName>
                                <DestCode>HAM</DestCode>
                                <Duration>01:45</Duration>
                                <FlightNo>3L370</FlightNo>
                                <DepDateTime>2014-08-08T06:10:00</DepDateTime>
                                <ArrDateTime>2014-08-08T07:55:00</ArrDateTime>
                                <Legs>
                                    <Leg>
                                        <Sequence>0</Sequence>
                                        <FlightNo>3L370</FlightNo>
                                        <DepCode>FDH</DepCode>
                                        <DepName>Friedrichshafen</DepName>
                                        <DestCode>HAM</DestCode>
                                        <DestName>Hamburg</DestName>
                                        <DepTime>06:10</DepTime>
                                        <ArrTime>07:55</ArrTime>
                                        <CarCode>3L</CarCode>
                                        <CarName>Inter Sky</CarName>
                                        <FareClass>Economy</FareClass>
                                        <ArrDateTime>2014-08-08T07:55:00</ArrDateTime>
                                        <DepDateTime>2014-08-08T06:10:00</DepDateTime>
                                    </Leg>
                                </Legs>
                                <Taxes>0</Taxes>
                                <FareADT>6500</FareADT>
                                <FareCHD>0</FareCHD>
                                <FareINF>0</FareINF>
                                <MiscFees>6400</MiscFees>
                                <Idx>307963</Idx>
                                <FareClass>Economy</FareClass>
                                <FareType>Web</FareType>
                                <FareId>3L0</FareId>
                            </Outbound>
                            <BagFee>0</BagFee>
                            <CcFee>600</CcFee>
                            <HandlingFee>500</HandlingFee>
                            <TotalFare>12900</TotalFare>
                            <FlightId>140514114543-02-21212-2</FlightId>
                            <Link2Book>http://sample.com</Link2Book>
                            <Provider>ElsyArres</Provider>
                        </Flight>
                        <Flight>
                            <Outbound>
                                <CarName>Inter Sky</CarName>
                                <CarCode>3L</CarCode>
                                <DepName>Friedrichshafen</DepName>
                                <DepCode>FDH</DepCode>
                                <DestName>Hamburg</DestName>
                                <DestCode>HAM</DestCode>
                                <Duration>01:45</Duration>
                                <FlightNo>3L376</FlightNo>
                                <DepDateTime>2014-08-08T18:00:00</DepDateTime>
                                <ArrDateTime>2014-08-08T19:45:00</ArrDateTime>
                                <Legs>
                                    <Leg>
                                        <Sequence>0</Sequence>
                                        <FlightNo>3L376</FlightNo>
                                        <DepCode>FDH</DepCode>
                                        <DepName>Friedrichshafen</DepName>
                                        <DestCode>HAM</DestCode>
                                        <DestName>Hamburg</DestName>
                                        <DepTime>18:00</DepTime>
                                        <ArrTime>19:45</ArrTime>
                                        <CarCode>3L</CarCode>
                                        <CarName>Inter Sky</CarName>
                                        <FareClass>Economy</FareClass>
                                        <ArrDateTime>2014-08-08T19:45:00</ArrDateTime>
                                        <DepDateTime>2014-08-08T18:00:00</DepDateTime>
                                    </Leg>
                                </Legs>
                                <Taxes>0</Taxes>
                                <FareADT>10500</FareADT>
                                <FareCHD>0</FareCHD>
                                <FareINF>0</FareINF>
                                <MiscFees>6400</MiscFees>
                                <Idx>307964</Idx>
                                <FareClass>Economy</FareClass>
                                <FareType>Web</FareType>
                                <FareId>3L0</FareId>
                            </Outbound>
                            <BagFee>0</BagFee>
                            <CcFee>600</CcFee>
                            <HandlingFee>500</HandlingFee>
                            <TotalFare>16900</TotalFare>
                            <FlightId>140514114543-02-21212-3</FlightId>
                            <Link2Book>http://sample.com</Link2Book>
                            <Provider>ElsyArres</Provider>
                        </Flight>
                    </Flights>
                </Response>
            </SoapMessage>
        </SearchFlightsResponse>
    </soap:Body>
</soap:Envelope>

package-info.java

@XmlSchema( 
    namespace = "http://testv80.elsyarres.net/service.asmx", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package com.myProject.wegolo

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

代码

        ...
        SOAPMessage sm = response;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        sm.writeTo(out);

        this.results = (SearchFlightsResponse) JAXB.unmarshal(sm.toString(), SearchFlightsResponse.class);

【问题讨论】:

    标签: java xml web-services jaxb unmarshalling


    【解决方案1】:

    您从 SOAP 服务收到的 XML 响应不是有效的 XML。 xmlns 属性必须是绝对的URI

    这意味着命名空间名称必须采用以下形式:&lt;scheme&gt;:&lt;scheme-specific-part&gt;

    为了使 XML 有效,您调用的服务应该将 urn:ElsyArres.API 作为命名空间值发回。

    <SearchFlightsResponse xmlns="urn:ElsyArres.API">
    

    您的 package-info 文件将如下所示:

    @XmlSchema( 
        namespace = "urn:ElsyArres.API", 
        elementFormDefault = XmlNsForm.QUALIFIED) 
    package com.myProject.wegolo
    
    import javax.xml.bind.annotation.XmlNsForm;
    import javax.xml.bind.annotation.XmlSchema;
    

    如果您无法让服务修复 URI 问题,您可以在通过使用 SAXFilter 进行解组时将其删除。这是一个很好的例子,它支持在解组过程中添加和删除命名空间:https://stackoverflow.com/a/2148541/73446

    【讨论】:

    • 我把它改成了 ElsyArres.API 但它仍然抛出同样的错误。
    • 感谢您的回答,但我不能要求他们更改服务,有什么方法可以处理错误?
    • 编辑了答案以包括如何在 jaxb 解析之前操作命名空间。
    • 我在代码中添加了过滤器但是遇到了错误,新的问题来了,请你看一下,谢谢stackoverflow.com/questions/24228672/…
    【解决方案2】:

    以下行是导致您出现问题的原因:

    JAXB.unmarshal(sm.toString(), SearchFlightsResponse.class);
    

    sm.toString() 的结果将类似于以下内容,而不是 XML 的 String 表示。

    com.sun.xml.internal.messaging.saaj.soap.ver1_2.Message1_2Impl@693f23a2
    

    您现在所做的相当于执行以下操作:

    import javax.xml.bind.JAXB;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXB.unmarshal("com.sun.xml.internal.messaging.saaj.soap.ver1_2.Message1_2Impl@693f23a2", Foo.class);  
        }
    }
    

    这将返回以下异常:

    Exception in thread "main" java.lang.IllegalArgumentException: URI is not absolute
        at java.net.URI.toURL(URI.java:1095)
        at javax.xml.bind.JAXB.toSource(JAXB.java:291)
        at javax.xml.bind.JAXB.unmarshal(JAXB.java:205)
        at forum23652823.Demo.main(Demo.java:8)
    

    注意

    JAXB.unmarshal(String, Class) 方法中的String 参数是 XML 的位置,而不是 XML 本身。这就是异常抱怨 URI 的原因。

    【讨论】:

      【解决方案3】:

      在进行解组时,我正在解析 xml 文件并给出唯一的类元素。 对于您的情况:将以下字符串提供给 unmarshaller,这可以为您节省许多无聊的错误。 之后,如果您遇到错误,您也可以尝试删除 xmlns="ElsyArres.API" 。命名空间的事情也可能产生错误。

          <SearchFlightsResponse xmlns="ElsyArres.API">
              <SoapMessage>
                  <Username>TEST</Username>
                  <Password>TESTs</Password>
                  <LanguageCode>EN</LanguageCode>
                  <ErrorMessage />
                  <ErrorCode>0</ErrorCode>
                  <AppVersion>8.0.3</AppVersion>
                  <Request>
                      <Departure>FDH</Departure>
                      <Destination>HAM</Destination>
                      <DepartureDate>2014-08-08</DepartureDate>
                      <ReturnDate />
                      <NumADT>1</NumADT>
                      <NumINF>0</NumINF>
                      <NumCHD>0</NumCHD>
                      <CurrencyCode>EUR</CurrencyCode>
                      <CarrierList />
                      <FareClasses />
                      <Providers />
                      <WaitForResult>true</WaitForResult>
                      <NearbyDepartures>true</NearbyDepartures>
                      <NearbyDestinations>true</NearbyDestinations>
                      <RROnly>false</RROnly>
                      <MetaSearch>true</MetaSearch>
                  </Request>
                  <Response>
                      <SearchFlightId>140514114543-02-43064-52</SearchFlightId>
                      <Roundtrip>false</Roundtrip>
                      <CurrencyCode>EUR</CurrencyCode>
                      <Flights>
                          <Flight>
                              <Outbound>
                                  <CarName>Inter Sky</CarName>
                                  <CarCode>3L</CarCode>
                                  <DepName>Friedrichshafen</DepName>
                                  <DepCode>FDH</DepCode>
                                  <DestName>Hamburg</DestName>
                                  <DestCode>HAM</DestCode>
                                  <Duration>01:45</Duration>
                                  <FlightNo>3L370</FlightNo>
                                  <DepDateTime>2014-08-08T06:10:00</DepDateTime>
                                  <ArrDateTime>2014-08-08T07:55:00</ArrDateTime>
                                  <Legs>
                                      <Leg>
                                          <Sequence>0</Sequence>
                                          <FlightNo>3L370</FlightNo>
                                          <DepCode>FDH</DepCode>
                                          <DepName>Friedrichshafen</DepName>
                                          <DestCode>HAM</DestCode>
                                          <DestName>Hamburg</DestName>
                                          <DepTime>06:10</DepTime>
                                          <ArrTime>07:55</ArrTime>
                                          <CarCode>3L</CarCode>
                                          <CarName>Inter Sky</CarName>
                                          <FareClass>Economy</FareClass>
                                          <ArrDateTime>2014-08-08T07:55:00</ArrDateTime>
                                          <DepDateTime>2014-08-08T06:10:00</DepDateTime>
                                      </Leg>
                                  </Legs>
                                  <Taxes>0</Taxes>
                                  <FareADT>6500</FareADT>
                                  <FareCHD>0</FareCHD>
                                  <FareINF>0</FareINF>
                                  <MiscFees>6400</MiscFees>
                                  <Idx>307963</Idx>
                                  <FareClass>Economy</FareClass>
                                  <FareType>Web</FareType>
                                  <FareId>3L0</FareId>
                              </Outbound>
                              <BagFee>0</BagFee>
                              <CcFee>600</CcFee>
                              <HandlingFee>500</HandlingFee>
                              <TotalFare>12900</TotalFare>
                              <FlightId>140514114543-02-21212-2</FlightId>
                              <Link2Book>http://sample.com</Link2Book>
                              <Provider>ElsyArres</Provider>
                          </Flight>
                          <Flight>
                              <Outbound>
                                  <CarName>Inter Sky</CarName>
                                  <CarCode>3L</CarCode>
                                  <DepName>Friedrichshafen</DepName>
                                  <DepCode>FDH</DepCode>
                                  <DestName>Hamburg</DestName>
                                  <DestCode>HAM</DestCode>
                                  <Duration>01:45</Duration>
                                  <FlightNo>3L376</FlightNo>
                                  <DepDateTime>2014-08-08T18:00:00</DepDateTime>
                                  <ArrDateTime>2014-08-08T19:45:00</ArrDateTime>
                                  <Legs>
                                      <Leg>
                                          <Sequence>0</Sequence>
                                          <FlightNo>3L376</FlightNo>
                                          <DepCode>FDH</DepCode>
                                          <DepName>Friedrichshafen</DepName>
                                          <DestCode>HAM</DestCode>
                                          <DestName>Hamburg</DestName>
                                          <DepTime>18:00</DepTime>
                                          <ArrTime>19:45</ArrTime>
                                          <CarCode>3L</CarCode>
                                          <CarName>Inter Sky</CarName>
                                          <FareClass>Economy</FareClass>
                                          <ArrDateTime>2014-08-08T19:45:00</ArrDateTime>
                                          <DepDateTime>2014-08-08T18:00:00</DepDateTime>
                                      </Leg>
                                  </Legs>
                                  <Taxes>0</Taxes>
                                  <FareADT>10500</FareADT>
                                  <FareCHD>0</FareCHD>
                                  <FareINF>0</FareINF>
                                  <MiscFees>6400</MiscFees>
                                  <Idx>307964</Idx>
                                  <FareClass>Economy</FareClass>
                                  <FareType>Web</FareType>
                                  <FareId>3L0</FareId>
                              </Outbound>
                              <BagFee>0</BagFee>
                              <CcFee>600</CcFee>
                              <HandlingFee>500</HandlingFee>
                              <TotalFare>16900</TotalFare>
                              <FlightId>140514114543-02-21212-3</FlightId>
                              <Link2Book>http://sample.com</Link2Book>
                              <Provider>ElsyArres</Provider>
                          </Flight>
                      </Flights>
                  </Response>
              </SoapMessage>
          </SearchFlightsResponse>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-09
        • 2019-10-07
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        • 2021-07-09
        相关资源
        最近更新 更多