Spring REST 2: POJO JSON Conversion

Spring and Jackson Support

  • When building Spring REST applications
    • Spring will automatically handle Jackson Integration
    • JSON data being passed to REST controller is converted to POJO
    • Java object being returned from REST controller is converted to JSON

      Set up

      Maven

      1
      2
      3
      4
      5
      <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.5</version>
      </dependency>

Create Student POJO Java Class

1
2
3
4
5
6
7
8
9
10
11
12
public class Student {

private int id;
private String firstName;
private String lastName;
private boolean active;

public Student() {

}
// Getters and Setters
}

Java POJO to JSON

Basically we are using the method writeValue() of Jackson pacakge.

1
2
3
4
5
6
7
8
9
ObjectMapper objectMapper = new ObjectMapper();
Student student = new Student();
student.id = 1;
student.firstName = "Xiaoke";
student.lastName = "Liu";
// write in a file
objectMapper.writeValue( new FileOutputStream("data/output-2.json"), student);
// Convert to String
String json = objectMapper.writeValueAsString(student);

JSON to Java POJO

Basically we are using the method readValue() of Jackson pacakge.

  1. Read Object From JSON File

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    public class Driver {

    public static void main(String[] args) {

    try {
    // create object mapper
    // ObjectMapper() from jackson.databind
    ObjectMapper mapper = new ObjectMapper();

    // read JSON file and map/convert to Java POJO:
    // data/sample-lite.json
    Student theStudent = mapper.readValue(
    new File("data/sample-lite.json"), Student.class);

    // print first name and last name
    System.out.println("First name = " + theStudent.getFirstName());
    System.out.println("Last name = " + theStudent.getLastName());

    }
    catch (Exception exc) {
    exc.printStackTrace();
    }
    }
    }
  2. Read Object From JSON Reader

    1
    2
    3
    4
    ObjectMapper objectMapper = new ObjectMapper();
    String studentJson = "{ \"id\" : \"001\", \"firstName\" : \"xiaoke\", \"lastName\" : \"Liu\" }";
    Reader reader = new StringReader(carJson);
    Student student = objectMapper.readValue(reader, Student.class);
  3. Read Object From JSON String

    1
    2
    3
    ObjectMapper objectMapper = new ObjectMapper();
    String studentJson = "{ \"id\" : \"001\", \"firstName\" : \"xiaoke\", \"lastName\" : \"Liu\" }";
    Student student = objectMapper.readValue(studentJson, Student.class);
  4. Read Object From JSON via URL

    1
    2
    3
    ObjectMapper objectMapper = new ObjectMapper();
    URL url = new URL("file:data/student.json");
    Student student = objectMapper.readValue(url, Student.class);

If JSON has property you don’t care about, which means that there is no corresponding field in POJO, use@JsonIgnoreProerties(ignoreUnknow=true) over POJO