先来看报错吧,如果错误信息一样就接着往下看吧
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `javax.xml.bind.JAXBElement` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1764)
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1209)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1415)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:362)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:195)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:402)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:195)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:402)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:195)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4593)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3601)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:378)
... 76 common frames omitted
实体类信息
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"dataSourceData",
"errorMessage"
})
@XmlRootElement(name = "getDataSourceResponse")
public class GetDataSourceResponse {
@XmlElement(nillable = true)
protected List<DataSourceData> dataSourceData;
@XmlElementRef(name = "errorMessage", namespace = "http://****.com/sd/pc/service", type = JAXBElement.class, required = false)
protected JAXBElement<PurchaseControlError3> errorMessage;
//get set 方法此处省略
}
报错的原因就是 jackson 无法反序列化 JAXBElement,因为它没有无参构造
从网上看了好多,大致有两种解决方案:
- 1:报错的实体类加上一个无参构造方法
- 2:配置Jackson的自定反序列化
上面这两种我从网上看到了好多这里就不赘述了,这里我用的时另外一种方法,因为我的实体类信息时,第二种配置起来也挺麻烦的. 然后就用梯子到墙外去找了一下,还真的找到了一种简单配置的方法,原链接:
Unmarshal/Convert JSON data to JAXBElement object
解决方案
- 创建一个替代类,用来狸猫换太子
// 创建一个相同构造方法类型的抽象类,用于替换库里无法添加无参构造的类
@JsonIgnoreProperties(value = {"globalScope", "typeSubstituted", "nil"})
public abstract class JAXBElementMixIn<T> {
@JsonCreator
public JAXBElementMixIn(@JsonProperty("name") QName name,
@JsonProperty("declaredType") Class<T> declaredType,
@JsonProperty("scope") Class scope,
@JsonProperty("value") T value) {
}
}
- 将上面的替代类配置到Jackson
//我这里需要全局使用,所以交给spring 托管
//-----如果你们局部使用,就不用加@Bean----
@Bean
public ObjectMapper objectMapper(){
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixIn(JAXBElement.class, JAXBElementMixIn.class);
return objectMapper;
}