Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore a specific node when parsing XML with Jackson

I want to know if it's possible to ignore one or many nodes when parsing XML using Jackson ML module.

I want to be able to parse this XML

<bundle>
  <id value="myBundleId"/>
  <meta>
    <profile value="http://myurl/profile1" />
    <profile value="http://myurl/profile2" />
    <tag>
      <system value="https://myurl/system" />
      <code value="myAppCode"/>
    </tag>
  </meta>
  <type value="message" />
</bundle>

into this POJO object

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

import lombok.Data;

@Data
public class Bundle {

    @JacksonXmlElementWrapper(localName = "id")
    @JacksonXmlProperty(isAttribute = true, localName = "value")
    private String id;

    @JacksonXmlElementWrapper(localName = "type")
    @JacksonXmlProperty(isAttribute = true, localName = "value")
    private String type;
}

Right now it's not working as I think the annotation @JacksonXmlElementWrapper is only working with lists.

It also gives me the following error message :

java.lang.IllegalArgumentException: Conflicting setter definitions for property "value"

like image 884
Fred Avatar asked Aug 08 '18 19:08

Fred


People also ask

Can Jackson parse XML?

Jackson is a library for handling JSON in Java systems and now has support for XML from version 2. DOM4J is a memory-efficient library for parsing XML, XPath, and XSLT (eXtensible Stylesheet Language).

What is Jackson Dataformat XML?

Data format extension for Jackson (http://jackson.codehaus.org) to offer alternative support for serializing POJOs as XML and deserializing XML as pojos. Support implemented on top of Stax API (javax. xml.

How to parse the XML document to Java objects using Jackson?

Parsing the XML document to Java objects using Jackson library is quite simple. The following is the XML that we are going to parse. To parse the above XML, we will use Jackson library. We have also included Apache Commons library for converting bytes to String. The maven dependency is as follows.

How to ignore unknown fields while parsing JSON in Java?

If a Model class is being created to represent the JSON in Java, then the class can be annotated as @JsonIgnoreProperties (ignoreUnknown = true) to ignore any unknown field. It means that if a new field is added later on JSON which represents this model then Jackson will not throw UnrecognizedPropertyException while parsing JSON in Java.

Why doesn't Jackson support JAXB XML?

This is the major way that the XML produced by Jackson is not compatible with the XML produced by JAXB. Of course, the behavior can be configured, using the JacksonXmlElementWrapperannotation for one field or the setDefaultUseWrapperconfiguration setting on the XmlMapperglobally. Jackson also has no support for working with specific XML Schemas.

Why does Jackson throw unrecognizedpropertyexception when parsing JSON?

For example, if you are consuming JSON from a REST Web Service and the next day they added a new field into JSON then the code will break because Jackson will throw UnrecognizedPropertyException and stop parsing JSON. This is troublesome and can cause problems in production if one is not aware.


1 Answers

Try the following:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Bundle {
   ...
}

Alternatively:

mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
like image 120
cassiomolin Avatar answered Oct 22 '22 23:10

cassiomolin