Use Your DTOs in Java Like a Pro
What is a DTO and the best ways to write your Data Transfer Objects in Java
If you are not a Medium member use this link to gain full access to the article:
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…