What is Jackson and what happens in @RestController when a method returns an object?
Options
- Jackson is a test runner, so the returned object is ignored
- Jackson is the default JSON library for converting objects (Correct option)
- Jackson encrypts the returned data before sending it out
- Jackson is a database where the object gets stored later
Answer
Jackson is the default JSON library: Spring uses it to convert the returned object to JSON (and request JSON to objects)
Why
Jackson (ObjectMapper) serializes Java objects to JSON and deserializes JSON to Java objects. Spring Boot includes it with the web starter.
Useful annotations: @JsonIgnore (hide a field), @JsonProperty("user_name") (rename), @JsonFormat (date format).
Common issue: serializing a JPA entity with bidirectional relations can loop forever; DTOs avoid it.
Remember
Jackson = Java object to/from JSON.