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

最佳答案

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

09-27 21:23