해당 포스팅에서는 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;
문제를 해결하기 위해 다음과 같은 방법들을 시도해 보았으나 해결되지 않았다.
@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.STRICT);
}
위와 같은 시도를 해봤지만 문제가 해결되지 않았다.
최종적으로 문제를 해결하기 위해 임시적으로 userId 필드명을 userIdentifier로 변경했다. 이로써 문제가 해결되었다.