先來(lái)看報(bào)錯(cuò)吧,如果錯(cuò)誤信息一樣就接著往下看吧
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
實(shí)體類信息
@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 方法此處省略
}
報(bào)錯(cuò)的原因就是 jackson 無(wú)法反序列化 JAXBElement,因?yàn)樗鼪](méi)有無(wú)參構(gòu)造
從網(wǎng)上看了好多,大致有兩種解決方案:
- 1:報(bào)錯(cuò)的實(shí)體類加上一個(gè)無(wú)參構(gòu)造方法
- 2:配置Jackson的自定反序列化
上面這兩種我從網(wǎng)上看到了好多這里就不贅述了,這里我用的時(shí)另外一種方法,因?yàn)槲业膶?shí)體類信息時(shí),第二種配置起來(lái)也挺麻煩的. 然后就用梯子到墻外去找了一下,還真的找到了一種簡(jiǎn)單配置的方法,原鏈接:
Unmarshal/Convert JSON data to JAXBElement object
解決方案
- 創(chuàng)建一個(gè)替代類,用來(lái)貍貓換太子
// 創(chuàng)建一個(gè)相同構(gòu)造方法類型的抽象類,用于替換庫(kù)里無(wú)法添加無(wú)參構(gòu)造的類
@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;
}