Home > Software engineering >  Why do we have to convert entity to dto?
Why do we have to convert entity to dto?

Time:04-05

I'm trying to understand Spring Boot and building Rest APIs.

I've seen some projects which is creates their own Converter class or which is using modelMapper. But I couldn't clearly get the main idea.

Why do we have to convert entities to DTOs? I could'nt find a lot of docs about it. Can you help me?

I've found two different things for mapping.

mapstruct and modelmapper. Which one should i use?

CodePudding user response:

DTO Represents Data Transfer Objects, They can be useful in many situation.

  1. Since the entity represents the database and has all fields created directly eg. a product entity has following schema

    name: "String", desc: "string", quantity_number: integer, quantity_qom: "string", amount_price: 200 amount_measure: "USD"

DTO helps us show response or request in a structured manner like

{ product:{
  item:{
  name:"string",
  desc:"string",
},
amount:{
  amount_price:200,
  amount_measure:"USD"
},
quantity:{
quantity_number:2,
quantity_qom:"Pcs"
 }
}
}

Regarding the usage i prefer modelmapper.

  • Related