Skip to content
EduVerse

DTO

Explained

Concept

Also known as: Data Transfer Object

An object that carries data between layers or systems, without business logic.

DTO, explained

Written by EduVerse

What it is

A DTO (Data Transfer Object) is a simple object whose only job is to carry data from one place to another, for example from your API to a client. It has fields and maybe some validation annotations, but no business logic. In Java you’ll often see them as records with names like CreateUserRequest or UserResponse.

Why teams use it

If your API returned your database entities directly, every change to a table would change your API, and fields like passwordHash could leak out by accident. A DTO decides exactly what goes in and what comes out. The cost is some mapping code between entities and DTOs, which libraries like MapStruct can generate for you.

An example from work

A GET /users/7 request in Postman returns the password hash next to the user’s name. The controller is returning the User entity itself. You create a UserResponse DTO with only the id, name and email, map the entity to it in the service, and the response now holds only what the client is meant to see.

Our own explanation, not a quote from the book.

Where it fits

Structured communication between controller and service, or between services

Coverage in the book

Explained

Explained and compared with alternatives, so you know where and why it’s used.

Appears in