Convert DTO to entity and vice versa

I am using Spring MVC architecture with JPA in my web application.

Where to convert DTO to an object and vice versa manually (without using any framework)?

+8
source share
5 answers

I think you are asking where to write the whole entity transformation logic -> DTO.

Like your essence

 class StudentEntity { int age ; String name; //getter //setter public StudentDTO _toConvertStudentDTO(){ StudentDTO dto = new StudentDTO(); //set dto values here from StudentEntity return dto; } } 

Your DTO should look like

 class StudentDTO { int age ; String name; //getter //setter public StudentEntity _toConvertStudentEntity(){ StudentEntity entity = new StudentEntity(); //set entity values here from StudentDTO return entity ; } } 

And your controller should look like

 @Controller class MyController { public String my(){ //Call the conversion method here like StudentEntity entity = myDao.getStudent(1); StudentDTO dto = entity._toConvertStudentDTO(); //As vice versa } } 
+9
source

This is an old question with an accepted answer, but you can update it in a simple way using the mapper model API.

 <dependency> <groupId>org.modelmapper</groupId> <artifactId>modelmapper</artifactId> <version>0.7.4</version> </dependency> 

Using this API, you avoid the manual installer and the receiver, as explained in the accepted answer.

In my opinion, both conversions should occur on the controller using private utilities and using the Java8 stream map (when exchanging the DTO collection), as shown in this article .

This must happen in the controller, because DTOs are for exclusive transmission objects. I do not take my DTO further.

You encode your services and data access layers on objects and convert DTOs to objects before calling service methods, and convert objects to DTOs before returning a response from the controller.

I prefer this approach because entities rarely change, and data can be added / removed from the DTO as desired.

Detailed configuration and model matching rules are described here.

+8
source

To my mind

  • Entity β†’ DTO conversion must be done in the controller before sending the jsp page
  • DTO β†’ Entity conversion should also be performed in the controller after checking the DTO returned from the jsp page

This gives you more control over the process, and you don’t need to change service / persistence classes every time the Entity logical fill changes.

+5
source

I can recommend using the mapstruct library:

 <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-jdk8</artifactId> <version>1.2.0.Final</version> </dependency> 

For example, if you have an entity like this:

 public class Entity { private Integer status; private String someString; private Date startDate; private Date endDate; // SKIPPED 

And DTO:

 public class Dto { private Boolean status; private String someString; private Long startDate; private Long endDate; // SKIPPED 

Then the conversion can be performed at the service level as follows:

 @Service public class SomeServiceImpl implements SomeService { @Autowired SomeDao someDao; @Autowired SomeMapper someMapper; public Dto getSomething(SomeRequest request) throws SomeException { return someDao.getSomething(request.getSomeData()) .map(SomeMapper::mapEntityToDto) .orElseThrow(() -> new SomeException("...")); } 

A cartographer can be represented as follows:

 @Mapper public interface SomeMapper { @Mappings( {@Mapping(target = "entity", expression = "java(entity.getStatus() == 1 ? Boolean.TRUE : Boolean.FALSE)"), @Mapping(target = "endDate", source = "endDate"), @Mapping(target = "startDate", source = "startDate") }) Dto mapEntityToDto(Entity entity); } 
+2
source

I suggest a different approach without additional dependency:

 import org.springframework.beans.BeanUtils ... BeanUtils.copyProperties(sourceObject, targetObject); 

It can be used to convert DTOs to entities or vice versa if they have the same property types and names.

If you want to ignore some fields, just add them after targetObject .

 BeanUtils.copyProperties(sourceObj, targetObj, "propertyToIgnoreA", "propertyToIgnoreB", "propertyToIgnoreC"); 

Source: http://appsdeveloperblog.com/dto-to-entity-and-entity-to-dto-conversion/

I think this is the cleanest way.

0
source

Source: https://habr.com/ru/post/982958/


All Articles