Sub topic 6
DTOs and JSON with Jackson
This chapter assumes you can write a @RestController with @GetMapping and @PostMapping, and that you know Java records. You will see how Spring Boot turns Java objects into JSON and back, and why the objects you expose should be separate DTOs.
What a DTO is
A DTO (data transfer object) is a class that describes the shape of data crossing the API boundary. It is not your database entity or your domain model. In this tutorial, CreateBookRequest is the input DTO and BookResponse is the output DTO. Records suit this well: they are immutable and short, and Jackson reads them natively.
Returning entities directly looks faster, but it causes problems:
- Internal fields, such as audit columns or password hashes, leak into the response.
- A database refactor silently changes the public JSON.
- Lazy-loaded relations can fail or trigger extra queries during serialization.
- One entity often needs different shapes for create, update, and read.
How Jackson fits in
spring-boot-starter-web includes Jackson through spring-boot-starter-json. Spring MVC calls a message converter backed by Jackson's ObjectMapper to read @RequestBody arguments and to write return values. Spring Boot configures this ObjectMapper for you.
Shaping the JSON
Annotations on the DTO control the output. This example runs on its own with jackson-databind on the classpath, which Spring Boot already provides.
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonDemo {
public record BookResponse(
long id,
String title,
@JsonProperty("author_name") String author,
@JsonInclude(JsonInclude.Include.NON_NULL) String subtitle) {}
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
BookResponse book = new BookResponse(1, "Dune", "Frank Herbert", null);
System.out.println(mapper.writeValueAsString(book));
// {"id":1,"title":"Dune","author_name":"Frank Herbert"}
}
}@JsonProperty renames a field in JSON. @JsonInclude drops a null value. @JsonIgnore hides a property entirely. A plain new ObjectMapper() does not carry Boot's defaults. Inside a Spring application, inject the Boot-configured mapper instead of creating your own.
Configuring Jackson globally
Prefer application.properties over annotations when a rule applies to every DTO:
spring.jackson.default-property-inclusion=non_null
spring.jackson.deserialization.fail-on-unknown-properties=trueBoot already writes dates such as LocalDate as ISO-8601 strings, for example "2026-12-01". It also ignores unknown JSON properties on input. The second line above turns that into an error, which catches client typos.
Common errors
| Symptom | Likely cause |
|---|---|
| 400 with `HttpMessageNotReadableException` | Malformed JSON, or a value of the wrong type, such as `"abc"` for a `long` |
| A field arrives as `null` or `0` | The JSON key does not match the DTO name, and Boot ignores unknown keys by default |
| 500 on a returned object | No accessible properties, or an entity with a lazy relation or a cycle |
| Extra fields in the response | An entity was returned instead of a DTO |
A missing key and an explicit null look the same to a record. Use Bean Validation, covered in a later chapter, to reject missing required fields.
Exercise
A client sends {"title": "Dune", "autor": "Frank Herbert"} to an endpoint that accepts CreateBookRequest(title, author) with default Spring Boot settings. What does the controller receive, and which setting would make the request fail instead?
Show answer
The request succeeds with title "Dune" and author null, because Boot ignores the unknown key "autor". Setting spring.jackson.deserialization.fail-on-unknown-properties=true makes Jackson reject the body, and the client gets a 400 response.