Spring REST 1: JSON

What is JSON?

  • JavaScript Object Notation
  • Lightweight data format for storing and exchanging data
  • JSON is plain text data

JSON Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
// Numbers: no quotes
"id": 14,
// String: in double quotes
"firstName": "Mario",
"lastName": "Rossi",
// Boolean: true / false
"active": true
// Nested JSON object
"address": {
"steet": "100 Main St",
"city": "Philadelphia",
"state": "Pennsylvania",
"zip": "19103",
"country": "USA"
}
// Array: use square brackets[...]
"languages": ["Java","C#","Python","Javascript"]
// null
}

JSON Data Binding

  • Data binding is the process of converting JSON data to a Java POJO, also know as Mapping, Serialization/Deserialization, Marshalling/Unmarshalling

    JSON Data Binding with Jackson

  • Spring uses the Jackson Project behind the scenes
  • Jackson handles data binding between JSON and Java POJO
  • By default, Jackson will call appropriate getter/setter method
    • JSON to Java POJO
      • call setter methods on POJO(automatically)
    • Java POJO to JSON
      • call getter methods on POJO(automatically)