ORVE WS (Dynamic) (4) Jackson XML mapper

0. Introduction

I love Jackson parser as it is easy to use and can be used to with some data formats such as CSVJSON, YAML and XML. Just define a wrapper and a class for marshalling/unmarshalling.

Although JAXB is a standard, in the Java ecosystem exists many solutions. And my decision is to use Jackson.

1. Defining MAVEN dependencies


To use Jackson XML it is required this dependency in Maven. Note that ${jackson.version} represents a variable that contains preferably the latest Jackson version.


1
2
3
4
5
    <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-xml</artifactId>
      <version>${jackson.version}</version>
    </dependency>

But if you want to use CSV, JSON or YAML in the project, it is as easy as including these dependencies


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<!-- Jackson for CSV --> 
<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-csv</artifactId>
  <version>${jackson.version}</version>
</dependency>

<!-- Jackson for JSON --> 
<groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>${jackson.version}</version>
</dependency>

<!-- Jackson for YAML --> 
<groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-yaml</artifactId>
  <version>${jackson.version}</version>
</dependency>


2. Marshalling/unmarshalling instances


The imports in the Java class are


1
2
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;

And now the mapper can be defined


1
2
3
4
  XmlMapper mapper = new XmlMapper();
  mapper.setDefaultUseWrapper(false); // No wrapper for List<> !!!!!!!
  mapper.registerModule(new JaxbAnnotationModule()); // Accept JAXB Annotations !!!!
   


In line 2, this sentence tells the wrapper NOT TO USE A FIELD WRAPPER. I have wasted nearly 2 days trying to unmarshal a class that some of its attributes where collections.

3. Wrapped and unwrapped XML formats


Here is a simple class showing the different XML formats for wrapped and unwrapped. The wrapped format has a tag that surrounds the collection.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package ximodante.orve.dynamicp;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

/**
 * Show the difference betweeen wrapped and unwrapped collections in XML
 * @author Ximo Dante
 *
 */
public class MyClass {
 public List<Integer> myIntList= new ArrayList<Integer>();
 
 public static void main(String[] args) { 
  MyClass c= new MyClass();
  c.myIntList.add(1);
  c.myIntList.add(2);
  c.myIntList.add(3);
  

  XmlMapper wrapMapper = new XmlMapper();
  wrapMapper.setDefaultUseWrapper(true);
  
  XmlMapper noWrapMapper = new XmlMapper();
  noWrapMapper.setDefaultUseWrapper(false); // No wrapper for List<> !!!!!!!
  
  try {
   System.out.println("wrapped collection......");
   System.out.println(wrapMapper.writeValueAsString(c));
   
   System.out.println("\nunwrapped collection......");
   System.out.println(noWrapMapper.writeValueAsString(c));
   
  } catch (JsonProcessingException e) {
   e.printStackTrace();
  }
 }
}

And the generated output is


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
wrapped collection......
<MyClass>
  <myIntList>
    <myIntList>1</myIntList>
    <myIntList>2</myIntList>
    <myIntList>3</myIntList>
  </myIntList>
</MyClass>

unwrapped collection......
<MyClass>
  <myIntList>1</myIntList>
  <myIntList>2</myIntList>
  <myIntList>3</myIntList>
</MyClass>


4. Accept JAXB annotations


This sentence tells the Jackson XML mapper to accept the annotations.


mapper.registerModule(new JaxbAnnotationModule()); // Accept JAXB Annotations !!!!

Now we can marshall and unmarshal most of the classes.



Comments

Popular posts from this blog

ENI (1) ENI Document OR the Spanish Electronic Administration Mafia

ORVE WS (Dynamic) (12) Defiining the control tables