2014-05-14 19 views
5

Ho ricevuto la risposta seguente da webservice e sto usando il seguente codice per annullare la risposta, ma restituisce l'errore seguente.JAXB restituisce unmarshalling 'URI non è assoluto' errore

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

risposta

<?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; 

Codice

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

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

risposta

1

La riga seguente è quello che si sta causando il problema:

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

Il risultato di sm.toString() sta per essere qualcosa di simile a quanto segue e non un String rappresentazione del XML.

[email protected]93f23a2 

Quello che state facendo ora è l'equivalente di fare la seguente:

import javax.xml.bind.JAXB; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXB.unmarshal("[email protected]93f23a2", Foo.class); 
    } 
} 

che restituirà la seguente eccezione:

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) 

Nota

Il String parametro nelIl metodoè il percorso dell'XML e non l'XML stesso. Questo è il motivo per cui l'eccezione si lamenta di un URI.

4

La risposta XML che si riceve dal servizio SOAP non è XML valido. L'attributo xmlns deve essere assoluto URI.

Ciò significa il nome del namespace deve essere sotto forma: <scheme>:<scheme-specific-part>

Affinché l'XML sia valida, il servizio che si sta chiamando deve essere rimandando urn:ElsyArres.API come il valore dello spazio dei nomi.

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

tuo file del pacchetto-info sarebbe quindi simile al seguente:

@XmlSchema( 
    namespace = "urn:ElsyArres.API", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package com.myProject.wegolo 

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

Se non è possibile ottenere il servizio per risolvere il problema URI è possibile rimuoverlo mentre unmarshalling attraverso l'uso di un SAXFilter. Ecco un ottimo esempio che supporta sia l'aggiunta che la rimozione di spazi dei nomi durante lo smantellamento: https://stackoverflow.com/a/2148541/73446

+0

ho cambiato in ElsyArres.API ma è ancora gettando stesso errore. – J888

+0

Ho modificato la mia risposta – gregwhitaker

+0

grazie per la risposta, ma non posso chiedere loro di cambiare il loro servizio, esiste un modo per gestire l'errore? – J888

0

Quando si esegue unmarshalling sto analizzando il file xml e dando l'unico elemento di classe. Per il tuo caso: dai la seguente stringa a unmarshaller, questo ti risparmia molti errori noiosi. Dopodiché, se ricevi un errore puoi provare a rimuovere anche xmlns = "ElsyArres.API". Anche le cose dello spazio dei nomi possono generare errori.

<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> 
Problemi correlati