JAXB返回相互的exculasive注释异常 [英] JAXB returns mutually exculasive annotations exception

查看:71
本文介绍了JAXB返回相互的exculasive注释异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下回复,并尝试使用JAXB解析它,但是下面的代码返回错误。

I am receiving following response and am trying to to parse it using JAXB but following code returns error below.

Exception in thread "main" org.springframework.http.converter.HttpMessageConversionException: Could not instantiate JAXBContext for class [class com.easytobook.EasytobookResponse]: 1 counts of IllegalAnnotationExceptions; nested exception is com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
com.easytobook.Roomtype#totalTaxesAndFees has mutually exclusive annotations @javax.xml.bind.annotation.XmlAttribute and @javax.xml.bind.annotation.XmlElement
    this problem is related to the following location:
        at @javax.xml.bind.annotation.XmlAttribute(name=EUR, required=false, namespace=##default)

我知道异常与totalTaxsesAndFees有关,但我不知道如何解决这个问题。

I know exception is related to totalTaxsesAndFees but I am not sure how to solve the issue.

EasytobookResponse response = restTemplate.postForObject(
                    url, easy, EasytobookResponse.class);

            System.err.println("RESPONSE >>>>"
                    + response.getResponse().getHotelInfo().get(0).getId());


@XmlRootElement(name = "EasytobookResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class EasytobookResponse {
    @XmlAttribute(name = "target")
    private String target;
    @XmlAttribute(name = "username")
    private String username;
    @XmlElement(name = "Response")
    private Response response;

    getters and setters 

@XmlRootElement(name = "Response")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
    @XmlElementWrapper(name="Hotelinfo")
    @XmlElement(name="Hotel")
    private List<Hotel> hotelInfo;

    getters and setters


@XmlRootElement(name = "Hotel")
@XmlAccessorType(XmlAccessType.FIELD)
public class Hotel {
    @XmlElement(name = "Id")
    private int id;
    @XmlElement(name = "FacilityGroups")
    private FacilityGroups facilityGroups;
    @XmlElement(name = "exact")
    private int exact;
    @XmlElementWrapper(name = "Roomtype")
    @XmlElement(name = "Roomtype")
    private List<Roomtype> roomType;

    getters and setters


@XmlRootElement(name = "FacilityGroups")
@XmlAccessorType(XmlAccessType.FIELD)
public class FacilityGroups {
    @XmlElement(name = "HandicapFacilities")
    private int handicapFacilities;
    @XmlElement(name = "HasInternet")
    private int hasInternet;
    @XmlElement(name = "HasParking")
    private int hasParking;
    @XmlElement(name = "PetsAllowed")
    private int petsAllowed;
    @XmlElement(name = "HasChildDiscount")
    private int hasChildDiscount;
    @XmlElement(name = "HasSwimmingPool")
    private int hasSwimmingPool;
    @XmlElement(name = "HasAirCon")
    private int hasAirCon;
    @XmlElement(name = "HasFitnessFacilities")
    private int hasFitnessFacilities;
    @XmlElement(name = "NonSmoking")
    private int nonSmoking;

    getters and setters

@XmlRootElement(name = "Roomtype")
@XmlAccessorType(XmlAccessType.FIELD)
public class Roomtype {
    @XmlAttribute(name = "parentid")
    private String parentid;
    @XmlElement(name = "Roomid")
    private long roomId;
    @XmlElement(name = "Roomname")
    private String roomName;
    @XmlElement(name = "Capacity")
    private int capacity;
    @XmlElement(name = "Available")
    private int available;

    @XmlElement(name = "Totalrate")
    private double totalRate;
    @XmlAttribute(name="currency")
    private String currency;
    @XmlAttribute(name="EUR")
    @XmlElement(name = "TotalTaxesAndFees")
    private double totalTaxesAndFees;
    @XmlElement(name = "TotalChargeable")
    private double totalChargeable;
    @XmlElement(name = "Reckratetotal")
    private double reckRateTotal;
    @XmlElement(name = "Breakfast")
    private int breakfast;
    @XmlElement(name = "Booklink")
    private String bookLink;
    @XmlElement(name = "Hoteldetailslink")
    private String hotelDetailsLink;
    @XmlElement(name = "Rtoken")
    private String rToken;
    @XmlElement(name = "Specialoffers")
    private String specialOffers;
    @XmlElement(name = "Bookingconditions")
    private BookingConditions bookingConditions;
    @XmlElement(name = "Rateinfo")
    private RateInfo rateInfo;

    getters and setters

@XmlRootElement(name = "Bookingconditions")
@XmlAccessorType(XmlAccessType.FIELD)
public class BookingConditions {
    @XmlElement(name = "Bookable")
    private int bookable;
    @XmlElement(name = "Chargepoint")
    private String chargePoint;
    @XmlElement(name = "Requirements")
    private Requirement requirement;

    getters and setters


@XmlRootElement(name = "Requirements")
@XmlAccessorType(XmlAccessType.FIELD)
public class Requirement {
    @XmlElement(name = "Capacity")
    private int capacity;
    @XmlElement(name = "Creditcardcvc")
    private int creditCardCvc;
    @XmlElement(name = "Billingaddress")
    private int billingAddress;

    getters and setters


@XmlRootElement(name = "Rateinfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class RateInfo {
    @XmlElement(name = "Underoccupancy")
    private int underoccupancy;
    @XmlElement(name = "Earlybooking")
    private int earlyBooking;
    @XmlElement(name = "Lastminutebooking")
    private int lastMinuteBooking;
    @XmlElement(name = "Nonrefundable")
    private int nonRefundable;
    @XmlElement(name = "Breakfastincluded")
    private int breakfastIncluded;

    getters and setters

响应

   <?xml version="1.0" encoding="UTF-8"?>
<EasytobookResponse>
    <Response target="test" username="project121">
        <Hotelinfo>
            <Hotel>
                <Id>436924</Id>
                <FacilityGroups>
                    <HandicapFacilities>0</HandicapFacilities>
                    <HasInternet>1</HasInternet>
                    <HasParking>1</HasParking>
                    <PetsAllowed>0</PetsAllowed>
                    <HasChildDiscount>0</HasChildDiscount>
                    <HasSwimmingPool>0</HasSwimmingPool>
                    <HasAirCon>1</HasAirCon>
                    <HasFitnessFacilities>0</HasFitnessFacilities>
                    <NonSmoking>1</NonSmoking>
                </FacilityGroups>
                <exact>436924</exact>
                <Roomtype>
                    <Roomid parentid="2069840686">2069840686</Roomid>
                    <Roomname>Two Persons Standard + Breakfast</Roomname>
                    <Capacity>2</Capacity>
                    <Available>8</Available>
                    <Totalrate currency="AUD" EUR="1204.3398">1800</Totalrate>
                    <TotalTaxesAndFees currency="AUD" EUR="0">0
                    </TotalTaxesAndFees>
                    <TotalChargeable currency="AUD" EUR="1204.3398">1800
                    </TotalChargeable>
                    <Reckratetotal currency="AUD" EUR="1204.3398">1800
                    </Reckratetotal>
                    <Breakfast>1</Breakfast>
                    <Booklink>https://stage-site.easytobook.us/booking_screen.php?hotel_id=436924&amp;exact=436924&amp;city_id=23&amp;arrival=20-11-2016&amp;departure=29-11-2016&amp;currency=EUR&amp;lang=en&amp;room[2069840686]=1&amp;persons=2&amp;rooms=1&amp;amu=280828207&amp;utm_source=project121&amp;utm_medium=affiliate&amp;utm_term=Sydney&amp;utm_content=etb5&amp;utm_campaign=en&amp;rtoken=1VSwnz7whjuxw-RGo5aP6cp-XmdDQSHC7twXmVrwPejOzYDFXdGY_bdbs9xbHuP8xU83qSmzJGz_vgQZHjeE5kVENJkuPBAtuKd6jTqIWCk,
                    </Booklink>
                    <Hoteldetailslink>http://stage-site.easytobook.us/hotel_proxy.php?hotel_id=436924&amp;lang=en&amp;arrival=20-11-2016&amp;departure=29-11-2016&amp;currency=EUR&amp;prs_arr%5B0%5D=2&amp;amu=280828207&amp;utm_source=project121&amp;utm_medium=affiliate&amp;utm_term=Sydney&amp;utm_content=etb5&amp;utm_campaign=en&amp;rtoken=1VSwnz7whjuxw-RGo5aP6cp-XmdDQSHC7twXmVrwPejOzYDFXdGY_bdbs9xbHuP8xU83qSmzJGz_vgQZHjeE5kVENJkuPBAtuKd6jTqIWCk,
                    </Hoteldetailslink>
                    <Rtoken>1VSwnz7whjuxw-RGo5aP6cp-XmdDQSHC7twXmVrwPejOzYDFXdGY_bdbs9xbHuP8xU83qSmzJGz_vgQZHjeE5kVENJkuPBAtuKd6jTqIWCk,
                    </Rtoken>
                    <Specialoffers />
                    <Bookingconditions>
                        <Bookable>1</Bookable>
                        <Chargepoint>hotel</Chargepoint>
                        <Requirements>
                            <Capacity>1</Capacity>
                            <Creditcardcvc>1</Creditcardcvc>
                            <Billingaddress>1</Billingaddress>
                        </Requirements>
                    </Bookingconditions>
                    <Rateinfo>
                        <Underoccupancy>0</Underoccupancy>
                        <Earlybooking>0</Earlybooking>
                        <Lastminutebooking>0</Lastminutebooking>
                        <Nonrefundable>0</Nonrefundable>
                        <Breakfastincluded>1</Breakfastincluded>
                    </Rateinfo>
                </Roomtype>
                <Roomtype>
                    <Roomid parentid="1379794752">1379794752</Roomid>
                    <Roomname>Two Persons Superior + Breakfast</Roomname>
                    <Capacity>2</Capacity>
                    <Available>4</Available>
                    <Totalrate currency="AUD" EUR="1264.5568">1890</Totalrate>
                    <TotalTaxesAndFees currency="AUD" EUR="0">0
                    </TotalTaxesAndFees>
                    <TotalChargeable currency="AUD" EUR="1264.5568">1890
                    </TotalChargeable>
                    <Reckratetotal currency="AUD" EUR="1264.5568">1890
                    </Reckratetotal>
                    <Breakfast>1</Breakfast>
                    <Booklink>https://stage-site.easytobook.us/booking_screen.php?hotel_id=436924&amp;exact=436924&amp;city_id=23&amp;arrival=20-11-2016&amp;departure=29-11-2016&amp;currency=EUR&amp;lang=en&amp;room[1379794752]=1&amp;persons=2&amp;rooms=1&amp;amu=280828207&amp;utm_source=project121&amp;utm_medium=affiliate&amp;utm_term=Sydney&amp;utm_content=etb5&amp;utm_campaign=en&amp;rtoken=ocGkvI7xuJJwz1BWWYNBztr7n-__tI8fVNfz3cZsrwRMGrtuHAEGziCH-0poK2ZoveEs-4Fz1_Y4U8pwE4KGKjJc4iwdSKM4ewIJwMU8omA,
                    </Booklink>
                    <Hoteldetailslink>http://stage-site.easytobook.us/hotel_proxy.php?hotel_id=436924&amp;lang=en&amp;arrival=20-11-2016&amp;departure=29-11-2016&amp;currency=EUR&amp;prs_arr%5B0%5D=2&amp;amu=280828207&amp;utm_source=project121&amp;utm_medium=affiliate&amp;utm_term=Sydney&amp;utm_content=etb5&amp;utm_campaign=en&amp;rtoken=ocGkvI7xuJJwz1BWWYNBztr7n-__tI8fVNfz3cZsrwRMGrtuHAEGziCH-0poK2ZoveEs-4Fz1_Y4U8pwE4KGKjJc4iwdSKM4ewIJwMU8omA,
                    </Hoteldetailslink>
                    <Rtoken>ocGkvI7xuJJwz1BWWYNBztr7n-__tI8fVNfz3cZsrwRMGrtuHAEGziCH-0poK2ZoveEs-4Fz1_Y4U8pwE4KGKjJc4iwdSKM4ewIJwMU8omA,
                    </Rtoken>
                    <Specialoffers />
                    <Bookingconditions>
                        <Bookable>1</Bookable>
                        <Chargepoint>hotel</Chargepoint>
                        <Requirements>
                            <Capacity>1</Capacity>
                            <Creditcardcvc>1</Creditcardcvc>
                            <Billingaddress>1</Billingaddress>
                        </Requirements>
                    </Bookingconditions>
                    <Rateinfo>
                        <Underoccupancy>0</Underoccupancy>
                        <Earlybooking>0</Earlybooking>
                        <Lastminutebooking>0</Lastminutebooking>
                        <Nonrefundable>0</Nonrefundable>
                        <Breakfastincluded>1</Breakfastincluded>
                    </Rateinfo>
                </Roomtype>
            </Hotel>
            .....


推荐答案

错误消息说明了一切:


com.easytobook。 Roomtype #totalTaxesAndFees 具有互斥注释@ javax.xml。 bind.annotation。 XmlAttribute 和@ javax.xml.bind.annotation。 XmlElement

com.easytobook.Roomtype#totalTaxesAndFees has mutually exclusive annotations @javax.xml.bind.annotation.XmlAttribute and @javax.xml.bind.annotation.XmlElement

您的代码有:

@XmlRootElement(name = "Roomtype")
@XmlAccessorType(XmlAccessType.FIELD)
public class Roomtype {
    . . .
    @XmlAttribute(name="EUR")
    @XmlElement(name = "TotalTaxesAndFees")
    private double totalTaxesAndFees;

这是不允许的。

您的问题是这些XML元素:

Your problem is these XML elements:

<Roomtype>
    <Roomid parentid="2069840686">2069840686</Roomid>
    . . .
    <Totalrate currency="AUD" EUR="1204.3398">1800</Totalrate>
    <TotalTaxesAndFees currency="AUD" EUR="0">0</TotalTaxesAndFees>
    <TotalChargeable currency="AUD" EUR="1204.3398">1800</TotalChargeable>
    <Reckratetotal currency="AUD" EUR="1204.3398">1800</Reckratetotal>

例如,以下字段注释告诉JAXB parentid Roomtype 的属性,当你想要它作为 Roomid 的属性时:

For example, the following field annotation tells JAXB that parentid is an attribute of Roomtype, when you want it as an attribute of Roomid:

public class Roomtype {
    @XmlAttribute(name = "parentid")
    private String parentid;

相反,为了在< Roomid> <上设置属性元素,你需要一个 Roomid 类(你不需要 @XmlRootElement ) :

Instead, in order to have an attribute on the <Roomid> element, you need a Roomid class (and you don't need the @XmlRootElement):

@XmlAccessorType(XmlAccessType.FIELD)
public class Roomid {
    @XmlAttribute(name = "parentid")
    private String parentid;
    @XmlValue
    private long roomId;
}

您的4个元素具有相同的结构,因此它们可以共享一个类:

Your 4 total elements have the same structure, so they can share a single class:

@XmlAccessorType(XmlAccessType.FIELD)
public class Total {
    @XmlAttribute(name = "currency")
    private String currency;
    @XmlAttribute(name = "EUR")
    private double eur;
    @XmlValue
    private double total;
}

然后使用这两个类:

@XmlAccessorType(XmlAccessType.FIELD)
public class Roomtype {
    @XmlElement(name = "Roomid")
    private Roomid roomid;
    . . .
    @XmlElement(name = "Totalrate")
    private Total totalrate;
    @XmlElement(name = "TotalTaxesAndFees")
    private Total totalTaxesAndFees;
    @XmlElement(name = "TotalChargeable")
    private Total totalChargeable;
    @XmlElement(name = "Reckratetotal")
    private Total reckratetotal;

这篇关于JAXB返回相互的exculasive注释异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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