I am writing simple blog web application using Spring MVC framework. I am willing to add DTO
layer to my app.
I decided to use ModelMapper framework for con
Let's assume you are reading from the database, and want to convert from entities in the DB to DTOs
The service layer might be separated from the controller (always the best approach), then do this:
Service layer code:
@Override
public List convertEntityToDTOList( ){
List post = postRepository.findAll();
Type listType = new TypeToken>(){}.getType();
List postDTOList = modelMapper.map(post, listType);
return postDTOList;
}
Then in your controller, add this:
public ResponseEntity> list(){
List postDTOList = iPost.convertEntityToDTOList();
return new ResponseEntity<>(postDTOList, HttpStatus.OK);
}
Note that the iPost is an interface that defines method. This is it:
public interface iPost {
List convertEntityToDTOList();
}
Credits to @André Carvalho