DTOs and the API model
What returning an entity actually sends, why the schema must not be the contract, and the opt-in shape that makes a leak impossible rather than unlikely.
The fastest way to build a REST endpoint is to return the entity. Spring will serialise it, Jackson will find the getters, and the response looks right in a browser. It is also how private data leaves your service, and the failure is a 200.
What returning an entity actually sends
Here is an ordinary JPA entity — the one from the JPA course, with the fields a real customer record carries:
@Entity
public class Customer {
@Id @GeneratedValue public Long id;
public String name;
public String email;
public String passwordHash; // nobody outside should ever see this
public boolean internalFlagged;
@OneToMany(mappedBy = "customer") public List<Order> orders = new ArrayList<>();
}And the endpoint everybody writes first:
@GetMapping("/customers/{id}")
public Customer get(@PathVariable Long id) { return customers.findById(id).get(); }$ curl -i /customers/1
HTTP/1.1 200
Content-Type: application/json
{"id":1,"name":"Ana","email":"ana@example.com","passwordHash":"$2a$10$SOMETHINGSECRET","internalFlagged":true,"orders":[]}The password hash and an internal flag went out over HTTP, and the response was a 200. Nothing failed. No log line. Jackson did exactly what it was asked: serialise every readable property of the object it was handed.
That is the whole argument, and it does not depend on anyone being careless. Nobody wrote passwordHash into a response. Somebody added a field to an entity, months later, in a different pull request — and every endpoint that returns that entity started publishing it.
Four more ways the same coupling bites
A field added to the database changes your public API. The entity is shaped by your schema; the response is shaped by your entity; so a migration is now an API change. That is exactly backwards: the schema should be the thing you are free to change, and the contract the thing you are not.
Lazy relationships explode during serialisation. The previous course's LazyInitializationException arrives from a new direction: Jackson walks orders, the persistence context is closed, and you get a 500 from the JSON writer. The usual fix — open-in-view, or making the relationship eager — makes the symptom stop by making every request slower.
Cycles. Customer.orders → Order.customer → Customer.orders. Jackson follows references until it runs out of stack. The workaround is annotations (@JsonIgnore, @JsonManagedReference) sprinkled on the entity, which means your persistence model now carries serialisation concerns.
You cannot have two shapes. The list screen wants a name; the detail screen wants everything. With one entity you get one shape, and the list endpoint ships fields nobody renders.
A DTO is the contract, written down
A DTO — a data transfer object — is a class whose only job is to be the shape of a request or a response:
public class CustomerView {
public final Long id;
public final String name;
public final String email;
public CustomerView(Long id, String name, String email) {
this.id = id; this.name = name; this.email = email;
}
}$ curl -i -X POST /customers -d '{"name":"Bo","email":"bo@example.com","age":30}'
HTTP/1.1 201
Location: /customers/2
{"id":2,"name":"Bo","email":"bo@example.com"}Three fields, because three fields are the contract. Adding passwordHash to the entity now changes nothing about what this endpoint returns, and that is the property worth paying for: the leak becomes impossible rather than unlikely.
Note also what is not here. The request body had age, the response does not. A request DTO and a response DTO are different shapes for good reasons — a client sends a password and never receives one; the server sends an id and never accepts one.
"But that is boilerplate"
It is, and the objection deserves a straight answer rather than a dismissal.
The mapping code is genuinely repetitive. What you are buying with it is that the shape of your API is a thing somebody chose, written in a file, visible in a diff, and reviewable. Every alternative gives that up to some degree:
- MapStruct generates the mapper at compile time. Real boilerplate removed, the DTO kept. This is the usual answer on a Java team and it is a good one.
- Projections straight from the repository — the previous course's interface projections — skip the entity entirely for read paths. The narrowest
select, no persistence context, no mapping code. For a list endpoint this is often the best answer of all. - Records, when your baseline allows them, make a response DTO a single line.
@JsonIgnoreon the entity is the one to be wary of. It works, and it means your API's shape is now scattered across annotations on a class whose purpose is persistence — and it is opt-out, so the next field somebody adds is public by default.
That last distinction is the one to carry: a DTO is opt-in — a field appears in your API because somebody wrote it there. Annotating the entity is opt-out — a field appears because nobody remembered to hide it. The 200 at the top of this lesson is what opt-out looks like in production.
Under the hood: why Jackson publishes a field without being told to
The "opt-out by default" behaviour this lesson keeps returning to is not a Jackson design flaw — it is the direct consequence of how ObjectMapper decides what counts as a property in the first place, and knowing the mechanism is what makes the DTO argument airtight rather than a matter of taste.
When Jackson serialises an object it does not consult anything you wrote about your API — it runs introspection: a BeanDescription, built by walking the class with reflection, collects every public getter (getX()/isX()), every public field with no corresponding getter, and every annotation it finds along the way, then caches that description per class for reuse. That BeanDescription is the list of properties Jackson will write — there is no separate, smaller "API surface" concept anywhere in the default path. A field is published because Jackson found it accessible, full stop; nothing has to ask Jackson to include it, which is exactly why adding passwordHash to Customer changed the response without anyone touching the controller. @JsonIgnore works by removing an entry from that introspected list after the fact — it is a correction applied after the automatic inclusion, not a gate the field has to pass before being included.
A DTO changes nothing about how Jackson works; it changes what Jackson is given to introspect. CustomerView has exactly three fields, so that is the entire BeanDescription, and there is no reflective path from CustomerView back to passwordHash for Jackson to discover, because the class itself does not have that field. This is the precise sense in which a DTO is opt-in: the introspection surface is the field list, chosen when the class was written, rather than whatever an entity happens to expose today.
Try it yourself
Trace the leak through the mechanism, not just the symptom
A teammate says: "I added @JsonIgnore on Customer.passwordHash, so returning the entity directly is fine now — no DTO needed." A second teammate adds a List<PaymentMethod> savedCards field to Customer three months later, with no @JsonIgnore, because it didn't occur to them that this class is ever serialised directly. Walk through Jackson's introspection as described above: does the first teammate's fix protect the second teammate's field?
What `@JsonIgnore` protects, and what it can never protect
No — @JsonIgnore only removes the one field it is written on from the BeanDescription; it does nothing to change how introspection treats every other field, including ones that don't exist yet. savedCards is a public getter Jackson's introspection will find exactly the way it found passwordHash originally, and it will be serialised by default because nobody told Jackson not to. The entity's shape is still the API's shape — @JsonIgnore is a per-field patch applied to a class whose job is persistence, and every new field starts unprotected until someone remembers to annotate it, which is precisely the opt-out failure mode this lesson already names. A DTO doesn't need this vigilance because savedCards being added to Customer has no effect on CustomerView's introspection at all — there is nothing to forget.
Misconceptions
- "MapStruct removes the risk, not just the boilerplate." It removes the boilerplate — a generated mapper still has to be told, field by field or by convention, what to copy, and a mapper written to copy "everything with a matching name" reintroduces the exact opt-out problem a hand-written DTO avoided, just one layer further from the entity. The safety comes from the DTO's own shape being deliberate, not from which tool populated it.
- "Jackson only serialises fields, so a getter-only computed property is safe to leave off." Introspection collects getters as readily as fields — a
getFullAccountHistory()method with no backing field is exactly as publishable as any field, because Jackson's rule is "what can I read from this object", not "what is stored in it". A DTO's safety comes from having no such method, not from having no such field.
Sources
- Jackson-databind project documentation —
ObjectMapper,BeanDescription, and property introspection. - Martin Fowler, Patterns of Enterprise Application Architecture — the Data Transfer Object pattern.
- Spring Framework reference documentation —
@RestControllerresponse serialisation viaHttpMessageConverter.