티스토리 뷰

728x90
반응형

Java 객체 매핑 라이브러리 중 하나인 Mapstruct를 사용해서 매핑할 때

@Builder로 선언된 객체를 매핑할 때는 Mapper 구현체에서 builder()를 사용하여 객체를 생성하게 된다.

@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class WishListDto {
    private Integer index;
    private String title;
    private String category;
    private String address;
    private String roadAddress;
    private String homePageLink;
    private String imageLink;
    private boolean isVisit;
    private int visitCount;
    private LocalDateTime lastVisitDate;
}
@Override
public WishListDto toDto(WishListEntity wishListEntity) {
    if ( wishListEntity == null ) {
        return null;
    }

    WishListDto.WishListDtoBuilder wishListDto = WishListDto.builder();	//Mapper 구현체에서 builder() 를 통한 객체 생성

    wishListDto.index( wishListEntity.getIndex() );
    wishListDto.title( wishListEntity.getTitle() );
    wishListDto.category( wishListEntity.getCategory() );
    wishListDto.address( wishListEntity.getAddress() );
    wishListDto.roadAddress( wishListEntity.getRoadAddress() );
    wishListDto.homePageLink( wishListEntity.getHomePageLink() );
    wishListDto.imageLink( wishListEntity.getImageLink() );
    wishListDto.visitCount( wishListEntity.getVisitCount() );
    wishListDto.lastVisitDate( wishListEntity.getLastVisitDate() );

    return wishListDto.build();
}

만약 Source 객체가 상속받은 객체인데 @Builder만 선언되어 있다면

부모 객체의 필드는 Builder로 생성되지 않기 때문에 Mapstruct로 매핑하면 부모 객체의 필드는 누락된다.

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SubWishDto extends WishListDto {
    private String subTitle;
}
@Override
public SubWishDto toDto(WishListEntity entity) {
    if ( entity == null ) {
        return null;
    }

    SubWishDto.SubWishDtoBuilder subWishDto = SubWishDto.builder();

    return subWishDto.build();
}

 

부모 객체의 상속받는 필드도 Builder 생성하려면 @SuperBuilder로 선언해야한다. (부모, 자식 객체 모두)

@NoArgsConstructor
@AllArgsConstructor
@Data
@SuperBuilder
public class WishListDto {
    private Integer index;
    private String title;
    private String category;
    private String address;
    private String roadAddress;
    private String homePageLink;
    private String imageLink;
    private boolean isVisit;
    private int visitCount;
    private LocalDateTime lastVisitDate;
}

@Getter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class SubWishDto extends WishListDto {
    private String subTitle;
}
@Override
public SubWishDto toDto(WishListEntity entity) {
    if ( entity == null ) {
        return null;
    }

    SubWishDto.SubWishDtoBuilder<?, ?> subWishDto = SubWishDto.builder();

    subWishDto.index( entity.getIndex() );	//부모 객체의 필드도 상속하여 builder로 생성
    subWishDto.title( entity.getTitle() );
    subWishDto.category( entity.getCategory() );
    subWishDto.address( entity.getAddress() );
    subWishDto.roadAddress( entity.getRoadAddress() );
    subWishDto.homePageLink( entity.getHomePageLink() );
    subWishDto.imageLink( entity.getImageLink() );
    subWishDto.visitCount( entity.getVisitCount() );
    subWishDto.lastVisitDate( entity.getLastVisitDate() );

    return subWishDto.build();
}

@Builer 패턴을 사용하지 않고 기본 생성자와 @Setter만 사용하면

Mapstruct에서는 부모 객체의 필드도 상속하여 setter()로 매핑된다.

 

 

https://mapstruct.org/documentation/stable/reference/html/#mapping-with-builders

 

MapStruct 1.5.5.Final Reference Guide

If set to true, MapStruct in which MapStruct logs its major decisions. Note, at the moment of writing in Maven, also showWarnings needs to be added due to a problem in the maven-compiler-plugin configuration.

mapstruct.org

 

728x90
반응형
반응형
300x250