【问题标题】:Java xml-java binding @XmlType.propOrderJava xml-java 绑定@XmlType.propOrder
【发布时间】:2015-03-16 19:45:38
【问题描述】:

Java-jersey 客户端和 jersy json 和 xml 绑定

如下回复

{"corp":"01105","rateCodeOffers":[{"rateCode":"!I","tosOffers":["MH0000010005"]}]}

映射类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
 "corp",
 "rateCodeOffers"
})
@XmlRootElement(name = "corpRateCodeTosOffers")
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
    public class CorpRcTosOffer implements Serializable {

    @XmlElement(required = true)
    private String corp;

    @XmlElement(required = true)
    private String errorMessage;

    @XmlElement(required = true)
    private String bRateCode;

    @XmlElement(required = true)
    private List<RatecodeTosOffers> rateCodeOffers;

    @XmlElement(required = false)
    private Map<String, List<TosConfirmSummary>> tosConfirmSummery;

    ... getter and setters
  }

要调用的Java代码

ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post(ClientResponse.class, payload);
 response.getEntity(CorpRcTosOffer.class);

我收到以下错误 b/c errorMessage/bRateCode/tosConfirmSummery 在响应 b/c 中不存在,它们是可选的,我应该怎么做才能消除以下错误。我可以只获取响应中可用的值。

javax.ws.rs.WebApplicationException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 5 counts of IllegalAnnotationExceptions
Property errorMessage is present but not specified in @XmlType.propOrder
    this problem is related to the following location:
        at private java.lang.String com.abc.app.CorpRcTosOffer.errorMessage
        at com.abc.app.CorpRcTosOffer
Property bRateCode is present but not specified in @XmlType.propOrder
    this problem is related to the following location:
        at private java.lang.String com.abc.app.CorpRcTosOffer.bRateCode
        at com.abc.app.CorpRcTosOffer
Property tosConfirmSummery is present but not specified in @XmlType.propOrder
    this problem is related to the following location:
        at private java.util.Map com.abc.app.CorpRcTosOffer.tosConfirmSummery
        at com.abc.app.CorpRcTosOffer
java.util.Map is an interface, and JAXB can't handle interfaces.
    this problem is related to the following location:
        at java.util.Map
        at private java.util.Map com.abc.app.CorpRcTosOffer.tosConfirmSummery
        at com.abc.app.CorpRcTosOffer
java.util.Map does not have a no-arg default constructor.
    this problem is related to the following location:
        at java.util.Map
        at private java.util.Map com.abc.app.CorpRcTosOffer.tosConfirmSummery
        at com.abc.app.CorpRcTosOffer

【问题讨论】:

    标签: java jaxb jersey-client xml-binding


    【解决方案1】:

    当您指定propOrder 时,您需要包含与其中的元素对应的所有映射字段/属性。这与值是否存在无关,只是如果值存在,它将以什么顺序出现。

    您需要按照例外情况进行操作,并将它们添加到propOrder

    Property bRateCode is present but not specified in @XmlType.propOrder
        this problem is related to the following location:
            at private java.lang.String com.abc.app.CorpRcTosOffer.bRateCode
            at com.abc.app.CorpRcTosOffer
    

    【讨论】:

      猜你喜欢
      • 2010-09-17
      • 2017-11-17
      • 2012-06-06
      • 2012-12-02
      • 2014-07-02
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      相关资源
      最近更新 更多