目前,我正在开发一个基于模式的 restFul 项目。因此,我们使用 JAXB 进行 XSD-->JAVA 转换。我有一堂课如下:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"systemResponse"
})
@XmlRootElement(name = "RestResponse")
public class RestResponse implements Serializable {
private final static long serialVersionUID = 1L;
@XmlElementRef(name = "SystemResponse", namespace = "http://www.intuit.com/psd/cdm/v1", type = JAXBElement.class)
protected JAXBElement<? extends CdmComplexBase> systemResponse;
...
}
以下是它的序列化方式:
{"systemResponse":{"name":"{http://www.intuit.com/psd/cdm/v1}Transactions","declaredType":"com.intuit.psd.cdm.v1.Transactions","scope":"javax.xml.bind.JAXBElement$GlobalScope","value":{"requestId":null,"requestName":null,"isEncrypted":null,"totalCount":null,"pageSize":null,"genDuration":null,"genDateTime":null,"transaction":[{"id":null,"externalKey":[],"metaData":null,"accountNumber":"12345678798","transactionNumber":null,"transactionReference":null,"batchID":null,"batchCycleDate":null,"paymentType":null,"paymentMethod":null,"transactionType":null,"cardType":null,"amount":null,"transactionDate":null,"authCode":null,"customerTransactionID":null,"ccnumberFirstSix":null,"ccnumberLastFour":null,"etctype":null,"posentryType":null}]},"nil":false,"globalScope":true,"typeSubstituted":false}}
尝试反序列化时,出现以下异常:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class javax.xml.bind.JAXBElement<com.intuit.psd.cdm.v1.CdmComplexBase>]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.StringReader@725d9aa7; line: 1, column: 20] (through reference chain: com.intuit.psd.cdm.v1.RestResponse["systemResponse"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:400)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:289)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:375)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:308)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2796)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1942)
at com.bp.samples.json.HelperJackson.testMarshalUnmarshal(HelperJackson.java:122)
at com.bp.samples.json.JSONToJavaTest.main(JSONToJavaTest.java:42)
谷歌搜索解决方案,建议您必须向序列化表单添加更多元数据或注册反序列化器。Jettison 正在使用 JAXB 注释来成功序列化和反序列化。另外,我也可以从序列化中删除命名空间“http://www.intuit.com/psd/cdm/v1”。有没有办法对 JACKSON 做同样的事情?
这是我用来配置 JACKSON 的代码:
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspectorPrimary = new JacksonAnnotationIntrospector();
AnnotationIntrospector introspectorSecondary = new JaxbAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(introspectorPrimary, introspectorSecondary);
mapper.getSerializationConfig().with(pair);
mapper.getDeserializationConfig().with(pair);