JAXB unmarshalling返回'URI不是绝对'错误 [英] JAXB unmarshalling returns 'URI is not absolute' error

查看:168
本文介绍了JAXB unmarshalling返回'URI不是绝对'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

响应

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


推荐答案

以下行是造成问题的原因:

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

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

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

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

这将返回以下异常:

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 参数 method是XML的位置,而不是XML本身。这就是为什么异常抱怨URI。

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

这篇关于JAXB unmarshalling返回'URI不是绝对'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆