Entity 와 Dto 매핑 시에 필드들을 하나하나 나열하기도 하지만,
라이브러리를 사용하여 진행하기도 하는 것 같다.
그래서 저번에 배워본 ModelMapper 를 이용하여 쉽게 변환하려 했다.
ModelMapper - Simple, Intelligent, Object Mapping.
Why ModelMapper? The goal of ModelMapper is to make object mapping easy, by automatically determining how one object model maps to another, based on conventions, in the same way that a human would - while providing a simple, refactoring-safe API for handli
modelmapper.org
Entity To DTO Conversion for a Spring REST API | Baeldung
How to to simpler conversions from Entities to DTOs and back in a Spring REST API.
www.baeldung.com
이렇게 Entity 와 Dto 의 필드명이 같은데도
해당 값들이 모두 null 로 처리되는 것을 볼 수 있었다.
Access level 이 public 인것을 볼 수 있다.
이를 Private 레벨로 바꾸면 setter 없이도 필드명이 같을 때 자동 매핑 처리를 할 수 있다.
'오답노트' 카테고리의 다른 글
java.util.ConcurrentModificationException 발생 (0) | 2021.01.28 |
---|---|
@Builder 사용시 초기화할 필드가 있다면 (0) | 2021.01.27 |
저장되지 않은 Entity 객체를 포함한 Entity 저장했을 때 (0) | 2021.01.27 |
TestRestTemplate webEnvironment 오류 (0) | 2021.01.22 |
@Entity, @Builder 함께 사용할 때 (0) | 2021.01.22 |