use*_*772 2 java mapping mapstruct
我正在努力将字符串对象从 source(Relation.class) 映射到目标列表(RelationListDTO.class) 。
关系.java
public class Relation {
private String name;
private String email;
private String completeAddress;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
关系列表DTO.java
public class RelationListDTO {
private String name;
private String email;
private List<Address> address;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
地址.java
public class Address{
private String street;
private String city;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
映射器类
@映射器
public interface RelationMapper {
@Mapping(source = "completeAddress", target = "address.get(0).city")
RelationListDTO relationToListDto(Relation relation);
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用。有人可以帮忙吗?
您尝试使用 MapStruct 执行的操作是不可能的。因为 MapStruct 不适用于运行时对象。MapStruct 只生成用于映射两个 bean 的纯 java 代码。我发现你的要求有点独特。您有一个地址列表,但只想从源对象映射城市?你仍然可以这样做
@Mapping( target = "address", source = "completeAddress")
RelationListDTO relationToListDto(Relation relation);
// MapStruct will know to use this method to map between a `String` and `List<Address>`
default List<Address> mapAddress(String relation){
//create new arraylist
// create new AddressObject and set completeAddress to address.city
// add that to list and return list
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11824 次 |
| 最近记录: |