需要使用 mapstruct 将两个源对象合并到目标对象中,这里 source1 中的一个字段有 List 而在 source2 中是字符串
Need to merge two source objects into target object using mapstruct ,here one field in source1 has List and in source2 that is string
public class Source1 {
private String name; //srihari
private List<String> city_names; //city_names.add("hyderabad-india")
}
public class Soruce2 {
private String name;
private String city_name; //hyderabad-india
private List<String> technologies; //Arrays.asList("java","mapstruct")
}
public class Target {
private String name; // Result: srihari
private String city_names; // Result: hyderabad-india
private String technologies; // Result: java, mapstruct
}`
list 只有一个值表示 list.size()=1。如果 source1 名称为空,则它必须取自 source2。并且 target 应该包含所有字段,即使这些字段在一个来源中不可用
尝试:
@Mapper
public interface MyMapper{
// will map all other fields that you specify
@Mapping( target = "city_names", ignore = true )
@Mapping( target = "technologies", ignore = true )
Target map(Source1 s1, Soruce2 s2);
default map(Source1 s1, Soruce2 s2, @MappingTarget Target t) {
// do whatever you like with city_names and technologies
}
}
public class Source1 {
private String name; //srihari
private List<String> city_names; //city_names.add("hyderabad-india")
}
public class Soruce2 {
private String name;
private String city_name; //hyderabad-india
private List<String> technologies; //Arrays.asList("java","mapstruct")
}
public class Target {
private String name; // Result: srihari
private String city_names; // Result: hyderabad-india
private String technologies; // Result: java, mapstruct
}`
list 只有一个值表示 list.size()=1。如果 source1 名称为空,则它必须取自 source2。并且 target 应该包含所有字段,即使这些字段在一个来源中不可用
尝试:
@Mapper
public interface MyMapper{
// will map all other fields that you specify
@Mapping( target = "city_names", ignore = true )
@Mapping( target = "technologies", ignore = true )
Target map(Source1 s1, Soruce2 s2);
default map(Source1 s1, Soruce2 s2, @MappingTarget Target t) {
// do whatever you like with city_names and technologies
}
}