无法解析响应中作为 START_OBJECT 或 START_ARRAY 返回的对象

Unable to parse object being returned as either START_OBJECT or START_ARRAY in the response

在 Expedia Affiliate Network 的一个 API 中,下面的部分响应作为数组返回给一些项目,而对于其他项目则返回对象,

"Surcharges": {
       "Surcharge": {
                "_type": "TaxAndServiceFee",
                "_amount": "11.94"
        },
        "_size": "1"
}

"Surcharges": {
       "Surcharge": [
            {
                    "_type": "TaxAndServiceFee",
                    "_amount": "11.94"
            },
            {
                    "_type": "Somethingelse",
                    "_amount": "11.94"
            }
        ],
        "_size": "1"
}

对应的JAXB模型为

@JsonPropertyOrder({
    "@size",
    "Surcharge"
})
@JsonIgnoreProperties(ignoreUnknown = true)
public class Surcharges {

    @JsonProperty("@size")
    private String Size;
    @JsonProperty("Surcharge")
    private Surcharge[] Surcharge;
    //was not part of the original model, added hoping this might solve :(
    @JsonProperty("Surcharge")
    private Surcharge Surcharge1;

    .....
}

解析失败

com.sun.jersey.api.client.ClientHandlerException: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of in.xxx.xxxxx.port.adapter.expedia.hotels.list.response.Surcharge out of START_ARRAY token

并将附加费更改为数组将导致

com.sun.jersey.api.client.ClientHandlerException: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of in.xxx.xxxxxx.port.adapter.expedia.hotels.list.response.Surcharge out of START_OBJECT token

处理此响应的最佳方法是什么

我可以在 Jackson Feature 配置中找到解决方案

ACCEPT_SINGLE_VALUE_AS_ARRAY,如何用Jersey的ClientConfig设置?

解决了使用客户端配置 JacksonJaxbProvider

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

JacksonJaxbJsonProvider jacksonProvider = new JacksonJaxbJsonProvider();
jacksonProvider.setMapper(mapper);
config.getSingletons().add(jacksonProvider);