JAXB unmarshalling returns 'URI is not absolute' error

折月煮酒 提交于 2019-12-07 06:01:15

问题


I received following response from webservice, and am using following code to unmarshal the response, but it returns following error.

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

Response

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

Code

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

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

回答1:


The following line is what's causing you the problem:

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

The result of sm.toString() is going to be something like the following and not a String representation of the XML.

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

What you are doing now is the equivalent of doing the following:

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);  
    }
}

Which will return the following exception:

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)

Note

The String parameter in the JAXB.unmarshal(String, Class) method is the location of the XML, and not the XML itself. This is why the exception is complaining about a URI.

  • http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/JAXB.html#unmarshal%28java.lang.String,%20java.lang.Class%29



回答2:


The XML response you are receiving back from the SOAP service is not valid XML. The xmlns attribute is required to be an absolute URI.

This means the namespace name must be in the form: <scheme>:<scheme-specific-part>

In order for the XML to be valid, the service you are calling should be sending back urn:ElsyArres.API as the namespace value.

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

Your package-info file would then look like the following:

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

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

If you cannot get the service to fix the URI issue you can remove it while unmarshalling through the use of a SAXFilter. Here is a great example that supports both adding and removing namespaces during unmarshalling: https://stackoverflow.com/a/2148541/73446




回答3:


When doing unmarshalling I am parsing the xml file and giving the only class element. For your case: give following string to unmarshaller, this saves you many boring errors. After that, if you are getting an error you can try to remove xmlns="ElsyArres.API" also. Namespace things may generate errors too.

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


来源:https://stackoverflow.com/questions/23652823/jaxb-unmarshalling-returns-uri-is-not-absolute-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!