com.fasterxml.jackson.dataformat.xml.XmlMapper Example

Q

How to use com.fasterxml.jackson.dataformat.xml.XmlMapper class?

✍: FYIcenter.com

A

com.fasterxml.jackson.dataformat.xml.XmlMapper class allows you to map a Java class object to a JSON message in a pretty format.

You can follow this tutorial to try it.

1. Write a sample Java program, XmlMapperWriter.java:

// XmlMapperWriter.java
// Copyright (c) FYIcenter.com

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class XmlMapperWriter {
  public static void main(String[] args) throws Exception {
    Person person = new Person();
    ObjectMapper mapper = new XmlMapper();
    String xml = mapper.writeValueAsString(person);
    System.out.println(xml);
  }

  public static class Person {
    private String name = "John Smith";
    private boolean married = false;
    private int age = 25;
    private String phone = null;

    public String getName() {
      return this.name;
    }
    public boolean getMarried() {
      return this.married;
    }
    public int getAge() {
      return this.age;
    }
    public String getPhone() {
      return this.phone;
    }
  }
}

2. Run this Java program with 4 Jackson JAR files. Too bad. It requires the org/codehaus/stax2/XMLInputFactory2 class.

fyicenter$ java -cp jackson-core-2.12.4.jar: \
  jackson-databind-2.12.4.jar: \
  jackson-annotations-2.12.4.jar \
  jackson-dataformat-xml-2.12.4.jar \
  XmlMapperWriter.java

Exception in thread "main" java.lang.NoClassDefFoundError: 
  org/codehaus/stax2/XMLInputFactory2
  ...
  at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1386)
  at java.xml/javax.xml.stream.FactoryFinder$1.run(FactoryFinder.java:348)
  ...

Go to Stax2 Maven Website. Download stax2-api-4.0.0.jar and included it in the classpath. Too bad. It requires another class.

fyicenter$ java -cp jackson-core-2.12.4.jar: \
  jackson-databind-2.12.4.jar: \
  jackson-annotations-2.12.4.jar \
  jackson-dataformat-xml-2.12.4.jar \
  stax2-api-4.0.0.jar \
  XmlMapperWriter.java

Exception in thread "main" java.lang.NoClassDefFoundError: 
  com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector
   ...
  at com.fasterxml.jackson.dataformat.xml.util.AnnotationUtil.findNamespaceAnnotation 
  at com.fasterxml.jackson.dataformat.xml.ser.XmlBeanSerializerModifier.changeProperties 
  at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.constructBeanOrAddOnSerializer 
  ...

Go to Jackson Maven Website. Download jackson-module-jaxb-annotations-2.12.4.jar and included it in the classpath.

fyicenter$ java -cp jackson-core-2.12.4.jar: \
  jackson-databind-2.12.4.jar: \
  jackson-annotations-2.12.4.jar \
  jackson-dataformat-xml-2.12.4.jar \
  stax2-api-4.0.0.jar \
  jackson-module-jaxb-annotations-2.12.4.jar \
  XmlMapperWriter.java

<Person xmlns="">
  <name>John Smith</name><married>false</married><age>25</age><phone/>
</Person>

The Java program mapped a Java class object to an XML message correctly.

 

⇒ Jackson - Java JSON library

⇐ com.fasterxml.jackson.databind.ObjectWriter Example

⇑ Using Jackson Java JSON library

⇑⇑ Jackson - Java JSON library

2021-08-11, 226👍, 0💬