我有包装器类:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Wrapper {
private String id;
private int number;
private Element internal;
}
我有内部课程:
@XmlRootElement
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment {
private BigDecimal amount;
private String account;
}
我创建Payment
,将其转换为org.w3c.dom.Element
并设置为Wrapper
。之后,我将 Wrapper转换为字符串,使用 Json Jackson
然后,我尝试以另一种方式转换-字符串到对象并得到错误。
public class Main {
public static void main(String[] args) throws JAXBException, ParserConfigurationException, IOException {
ObjectMapper mapper = new ObjectMapper();
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
JAXBContext context = JAXBContext.newInstance(Payment.class);
Marshaller marshaller = context.createMarshaller();
//create paymant
Payment payment = new Payment(BigDecimal.valueOf(100), "666");
//convert to Element
marshaller.marshal(payment, document);
Element element = document.getDocumentElement();
//set Paymant as Element to Wrapper
Wrapper wrapper = new Wrapper("123321", 987, element);
//convert Wrapper to string
String wrapperAsString = mapper.writeValueAsString(wrapper);
System.out.println(wrapperAsString);
//convert the other way
Wrapper restoreWrapper = mapper.readValue(wrapperAsString, Wrapper.class);
}
}
将包装器转换为字符串时,会得到以下结果:
{
"id": "123321",
"number": 987,
"internal": "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<payment><account>666</account><amount>100</amount></payment>"
}
但是当以另一种方式转换时,我得到了异常:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Problem deserializing property 'internal' (expected type: [simple type, class org.w3c.dom.Element]; actual type: `com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl`), problem: argument type mismatch
at [Source: (String)"{"id":"123321","number":987,"internal":"<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<payment><account>666</account><amount>100</amount></payment>"}"; line: 1, column: 40] (through reference chain: com.pavel.Wrapper["internal"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:278)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty._throwAsIOE(SettableBeanProperty.java:597)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:141)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
at com.pavel.Main.main(Main.java:36)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:139)
答案 0 :(得分:1)
您有两个选择:
internal
的类型更改为Document
或Node
。Element element = document.getDocumentElement()
; 自定义反序列化器:
class ElementDeserializer extends DOMDeserializer<Element> {
private static final long serialVersionUID = 1L;
public ElementDeserializer() {
super(Element.class);
}
@Override
public Element _deserialize(String value, DeserializationContext ctxt)
throws IllegalArgumentException {
return parse(value).getDocumentElement();
}
}
您可以按以下方式注册它:
SimpleModule elementModule = new SimpleModule();
elementModule.addDeserializer(Element.class, new ElementDeserializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(elementModule);
但是我建议保留Documen
-com.fasterxml.jackson.databind.ext.DOMDeserializer.DocumentDeserializer
和Node
-com.fasterxml.jackson.databind.ext.DOMDeserializer.NodeDeserializer
的自定义反序列器。