WindsomeProject - ModelMapper 변환 오류

박민수·2023년 12월 20일
0

WindsomeProject

목록 보기
3/32
post-thumbnail

개요

해당 포스팅에서는 DTO(ProfileForm)와 Entity(Account) 두 객체를 ModelMapper를 사용하여 변환할 때 발생한 문제와 그 해결 과정에 대해 다뤄보고자 한다.

오류

DTO(ProfileForm)와 Entity(Account) 두 객체를 ModelMapper를 통해 변환 할 때, DTO의 userId 필드가 Entity의 userId 필드에 매핑되지 않고, id(기본 키) 필드에 매핑되어 변환을 시도해서 NumberFormatException이 발생한다.

/* Account.java */

@Entity
public class Account extends BaseTimeEntity {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "account_id")
    private Long id;

    @Column(unique = true)
    private String userId;
}
/* ProfileFormDto.java */

@Data
public class ProfileFormDto {
    private String userId;

시도한 방법

문제를 해결하기 위해 다음과 같은 방법들을 시도해 보았으나 해결되지 않았다.

  1. Entity의 id 필드를 매핑 과정에서 스킵하도록 설정
  2. 매칭 전략(MatchingStrategy.Strict) 설정
@Bean
public ModelMapper modelMapper() {
    ModelMapper modelMapper = new ModelMapper();
    modelMapper.getConfiguration()
        .setMatchingStrategy(MatchingStrategies.STRICT);
}

위와 같은 시도를 해봤지만 문제가 해결되지 않았다.

해결책

최종적으로 문제를 해결하기 위해 임시적으로 userId 필드명을 userIdentifier로 변경했다. 이로써 문제가 해결되었다.

profile
안녕하세요 백엔드 개발자입니다.

0개의 댓글

관련 채용 정보