liveBook · Manning

Data transfer object

As explained in Spring Start Here, Second Edition

A data transfer object is an object that carries structured data between software applications or application boundaries.

A data transfer object (DTO) carries structured data across an application boundary, such as between a REST endpoint and a client. It can model either an HTTP request body or a response body. section 10.3.1 section 13.3

In a Spring REST endpoint, a DTO can serve either direction:

  • Return a Country object to send its name and population in the response. section 10.3.1
  • Accept a TransferRequest object to model the data in a transfer request body. section 13.3
Listing 13.10 The TransferRequest data transfer object modeling the HTTP request body
public class TransferRequest {
 
  private long senderAccountId;
  private long receiverAccountId;
  private BigDecimal amount;
 
  // Omitted code
}

The TransferRequest DTO holds the sender account ID, receiver account ID, and transfer amount. section 13.3

A DTO returned as JSON

Returning a Country for France with population 67 produces a response body with the attributes "name" and "population". A collection of Country objects can likewise be returned as a JSON array. section 10.3.1

How Spring Start Here, Second Edition covers it

How deep it goes
Defines DTOs through request and response examples and shows JSON formatting, while deferring JSON basics to an appendix.
Examples
Uses Country response objects and a TransferRequest body carrying account IDs and an amount.
What you can do after
Apply DTOs in Spring REST controllers by returning response objects or accepting request-body objects.
How it connects
Connects DTOs with REST endpoints, HTTP request and response bodies, and JSON.

Also explained in

Compare how 10 books explain data transfer object →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.