将具有org.w3c.dom.Element的Java对象转换为字符串,并在转换回时得到错误

时间:2019-03-18 08:58:29

标签: java json xml dom jackson

我有包装器类:

@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)

1 个答案:

答案 0 :(得分:1)

您有两个选择:

  1. internal的类型更改为DocumentNode
  2. 编写自定义反序列化器,它将与您在以下行中所做的相同: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.DocumentDeserializerNode-com.fasterxml.jackson.databind.ext.DOMDeserializer.NodeDeserializer的自定义反序列器。