What is dto in java

Last updated: April 1, 2026

Quick Answer: DTO in Java is a Data Transfer Object, a simple Java class containing private fields with public getter and setter methods, used to transfer data between different layers of an application without any business logic.

Key Facts

Understanding DTOs in Java

A DTO (Data Transfer Object) in Java is a plain Java class specifically designed to hold and transfer data between different layers of an application. A typical Java DTO contains private fields representing the data, along with public getter and setter methods to access and modify that data. The key characteristic of a Java DTO is its simplicity—it contains no business logic, no methods for performing operations, and no complex behaviors. Instead, it focuses solely on encapsulating and transferring data.

Structure of a Java DTO

A typical Java DTO follows a simple structure. It includes private member variables that represent the data being transferred, a no-argument constructor (required by many frameworks), getter methods (typically prefixed with 'get') to retrieve field values, and setter methods (typically prefixed with 'set') to assign values. Many Java developers also implement equals(), hashCode(), and toString() methods for easier debugging and comparison. DTOs may include validation annotations when used with frameworks like Spring or Hibernate, but they avoid implementing complex methods or business logic.

DTOs in Web Applications

In modern Java web applications, DTOs are essential for REST API development. When a client sends a request to a REST endpoint, the incoming JSON data is automatically deserialized into a DTO by frameworks like Spring. Similarly, when returning data from an API endpoint, domain objects are mapped to DTOs, which are then serialized to JSON. This separation ensures that clients cannot accidentally access internal domain object properties and that changes to internal data models don't break API contracts.

Mapping Between DTOs and Domain Objects

Java applications typically require converting between DTOs and domain entities. While this can be done manually through setter calls, most modern applications use mapping frameworks like MapStruct or Modelmapper to automatically convert between DTOs and domain objects. Spring Data also provides automatic mapping capabilities. This mapping layer ensures that domain logic remains independent from API representations, allowing the API to evolve without affecting core business logic.

Best Practices for Java DTOs

Effective DTOs should be tailored to their specific use case. A single entity might have multiple DTOs: a CreateUserDTO for registration, a UserResponseDTO for retrieving user information, and a UserListDTO for list operations with fewer fields. DTOs should be immutable when possible to prevent accidental modifications. Input DTOs should include validation annotations like @NotNull and @Email to validate incoming data at the API boundary. DTOs should never contain sensitive information that shouldn't be exposed through APIs, such as password hashes or internal identifiers.

Related Questions

Should I use domain objects directly in my REST APIs?

No, this is considered poor practice. Exposing domain objects risks revealing internal implementation details, making API evolution difficult. DTOs provide a stable contract between clients and servers.

How do I map DTOs to domain objects in Spring?

Spring supports automatic mapping through libraries like MapStruct, Modelmapper, or manual mapping in service classes. Spring Data also provides automatic conversion capabilities for common scenarios.

What annotations should DTOs include?

DTOs should include validation annotations like @NotNull, @Email, @Size for input validation. They may include @JsonProperty for JSON field name mapping and @JsonIgnore to exclude fields from serialization.

Sources

  1. Wikipedia - Data Transfer Object CC-BY-SA-4.0