728x90
반응형
source 는 Github 에 있습니다.
목차는 spring series 목차 에 있습니다.
[spring 21편] mapstruct 활용 2번째 (expression 사용)
mapstruct expression 활용
- expression 을 통해 source list --> target list 으로 convert 하는 예제를 정리했습니다.
Source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ItemInfoMapper.java | |
@Mapper( | |
componentModel = "spring", | |
injectionStrategy = InjectionStrategy.CONSTRUCTOR, | |
unmappedTargetPolicy = ReportingPolicy.ERROR | |
) | |
public interface ItemInfoMapper { | |
ItemInfoMapper INSTANCE = Mappers.getMapper(ItemInfoMapper.class); | |
@Mapping(source = "id", target = "itemId") | |
@Mapping(expression = "java(Item.resolveDescList(item))", target = "descList") | |
ItemInfo.Base of(Item item); | |
} | |
// Item.java | |
@Data | |
@Entity | |
@Builder | |
@AllArgsConstructor | |
@NoArgsConstructor | |
public class Item { | |
@Id | |
private String id; | |
@Column | |
private String itemName; | |
private List<ItemDescription> descriptionList; | |
public static List<ItemInfo.Sub> resolveDescList(final Item item) { | |
return item.getDescriptionList().stream().map(desc -> | |
ItemInfo.Sub.builder() | |
.desc1(desc.getDesc1()) | |
.desc2(desc.getDesc2()) | |
.desc3(desc.getDesc3()) | |
.build() | |
).collect(Collectors.toList()); | |
} | |
; | |
@Data | |
@AllArgsConstructor | |
@Builder | |
public static class ItemDescription { | |
private String desc1; | |
private String desc2; | |
private String desc3; | |
} | |
} | |
// ItemInfo.java | |
public class ItemInfo { | |
@Data | |
@Builder | |
@AllArgsConstructor | |
@NoArgsConstructor | |
public static class Base { | |
private String itemId; | |
private String itemName; | |
List<Sub> descList; | |
} | |
@Data | |
@SuperBuilder | |
@EqualsAndHashCode(callSuper = true) | |
public static class Sub extends SuperSub { | |
private String etc1; | |
} | |
@NoArgsConstructor | |
@AllArgsConstructor | |
@Data | |
@SuperBuilder | |
public static class SuperSub { | |
private String desc1; | |
private String desc2; | |
private String desc3; | |
} | |
} | |
// ItemInfoMapperTest.java | |
public class ItemInfoMapperTest { | |
@Test | |
void testItemInfoMapperTest() { | |
final var item = Item.builder() | |
.id("10") | |
.itemName("testItem") | |
.descriptionList(List.of( | |
Item.ItemDescription.builder() | |
.desc1("desc1") | |
.desc2("desc2") | |
.desc3("desc3") | |
.build(), | |
Item.ItemDescription.builder() | |
.desc1("desc11") | |
.desc2("desc22") | |
.desc3("desc33") | |
.build() | |
)) | |
.build(); | |
final ItemInfo.Base result = ItemInfoMapper.INSTANCE.of(item); | |
final ItemInfo.Base expected = getExpected(); | |
Assertions.assertEquals(result, expected); | |
} | |
private ItemInfo.Base getExpected() { | |
final var expected = ItemInfo.Base.builder() | |
.itemId("10") | |
.itemName("testItem") | |
.descList(List.of( | |
ItemInfo.Sub.builder() | |
.desc1("desc1") | |
.desc2("desc2") | |
.desc3("desc3") | |
.build(), | |
ItemInfo.Sub.builder() | |
.desc1("desc11") | |
.desc2("desc22") | |
.desc3("desc33") | |
.build())) | |
.build(); | |
return expected; | |
} | |
} | |
'Spring > series' 카테고리의 다른 글
[spring 23편] spring boot 3.2.1 적용 회고 (0) | 2024.02.20 |
---|---|
[spring 22편] 멀티스레드 환경 로그 식별 (MDC, ThreadLocal) (0) | 2023.10.06 |
[spring 20편] spring validate custom (spel 이용해서 validate 수행할지 결정) (0) | 2023.01.13 |
[spring 19편] spring validate custom (유효성 검사) (0) | 2023.01.01 |
[spring 18편] spring validate (유효성 검사) (0) | 2023.01.01 |
댓글