Use Your DTOs in Java Like a Pro

What is a DTO and the best ways to write your Data Transfer Objects in Java

Kacper Wójcicki
Javarevisited

--

Photo by James Harrison on Unsplash

If you are not a Medium member use this link to gain full access to the article:

https://medium.com/javarevisited/use-your-dtos-in-java-like-a-pro-1cd3f6b2fda8?sk=27cfbd023fd601ebff88fa61d15dc898

Introduction

When writing a REST API there is often a situation when we need to return objects that the user asked for or that we need to take an object from the user and use it to create an object to save in the database.

Probably there is a model, maybe a hibernate entity, something that simplifies the reality, but when we want to expose it with the API sometimes we don’t want to expose the exact model or we don’t need all the fields of the entity to create a new one or maybe you want to expose data from two entities as one object?

That’s where the Data Transfer Objects design pattern comes in.

What’s a DTO?

DTO (Data Transfer Object) is an object used to send and receive data. It is a common design pattern used by many software developers. It is used to model a response or request body the way the developer wants it when fields differ from the model. We can get rid of…

--

--

Kacper Wójcicki
Javarevisited

Java Fullstack Developer | 23 yo | Let’s talk about software! Visit my personal site😉 https://kacper-wojcicki.ovh