0

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
4

1 回答 1

2

当您指定 a 时,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
于 2015-03-17T10:45:20.733 回答