如何使用modelMapper转换嵌套类
How to use modelMapper to convert nested classes
我有一个简单的 class,我想使用 modelMapper 映射到 DTO class。
class Source {
private String name;
private String address;
List<Thing> things;
// getters and setters follows
}
class Thing {
private String thingCode;
private String thingDescription;
// getters and setters
}
我想将它们转换为包含 ThingDTO 列表的 sourceDTO,例如
class sourceDTO {
private String name;
private String address;
List<ThingDTO> things;
// getters and setters.
}
class ThingDTO {
private String thingCode;
private String thingDescription;
// getters and setters
}
如果我删除我的 Things 列表和 ThingsDTO 列表,那么使用 modelmapper 会很愉快,
modelMapper.map(source, SourceDTO.class);
但我不知道如何让映射器将事物列表转换为 ThingDTO 列表。从文档中,我认为我需要创建一个扩展 PropertyMap 的映射器 class,但我不知道如何配置它。
欢迎任何指向相关文档的指针
我认为如果您将 ModelMapper 配置为 LOOSE 或 STANDARD,它会为您做。
modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE);
否则你可以尝试下一个:
您可以像这样创建一个转换器:
public class ListThingToThingDTOConverter implements Converter<List<Thing>, List<ThingDTO>> {
@Override
public List<ThingDTO> convert(MappingContext<List<Thing>, List<ThingDTO>> context) {
List<Thing> source = context.getSource();
List<ThingDTO> output = new ArrayList<>();
...
//Convert programmatically List<Thing> to List<ThingDTO>
...
return output;
}}
然后自定义一个映射事物到 ThingDTO,如下所示:
public class SourceToSourceDTOMap extends PropertyMap<Thing, ThingDTO> {
@Override
protected void configure(){
using(new ListThingToThingDTOConverter()).map(source.getThings()).setThings(null);
}
最后,您必须将 SourceToSourceDTOMap 添加到您的 ModelMapper,如下所示:
modelMapper = new ModelMapper();
modelMapper.addMappings(new SourceToSourceDTOMap());
您可以通过创建泛型来像下面的代码那样进行映射。 link供参考
http://modelmapper.org/user-manual/generics/
进口:
import java.lang.reflect.Type;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
在您的服务或控制器中class:
ModelMapper modelMapper = new ModelMapper();
Type listType = new TypeToken<SourceDTO>(){}.getType();
SourceDTO sourceDTO = modelMapper.map(source,listType);
我有一个简单的 class,我想使用 modelMapper 映射到 DTO class。
class Source {
private String name;
private String address;
List<Thing> things;
// getters and setters follows
}
class Thing {
private String thingCode;
private String thingDescription;
// getters and setters
}
我想将它们转换为包含 ThingDTO 列表的 sourceDTO,例如
class sourceDTO {
private String name;
private String address;
List<ThingDTO> things;
// getters and setters.
}
class ThingDTO {
private String thingCode;
private String thingDescription;
// getters and setters
}
如果我删除我的 Things 列表和 ThingsDTO 列表,那么使用 modelmapper 会很愉快,
modelMapper.map(source, SourceDTO.class);
但我不知道如何让映射器将事物列表转换为 ThingDTO 列表。从文档中,我认为我需要创建一个扩展 PropertyMap 的映射器 class,但我不知道如何配置它。
欢迎任何指向相关文档的指针
我认为如果您将 ModelMapper 配置为 LOOSE 或 STANDARD,它会为您做。
modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE);
否则你可以尝试下一个:
您可以像这样创建一个转换器:
public class ListThingToThingDTOConverter implements Converter<List<Thing>, List<ThingDTO>> { @Override public List<ThingDTO> convert(MappingContext<List<Thing>, List<ThingDTO>> context) { List<Thing> source = context.getSource(); List<ThingDTO> output = new ArrayList<>(); ... //Convert programmatically List<Thing> to List<ThingDTO> ... return output; }}
然后自定义一个映射事物到 ThingDTO,如下所示:
public class SourceToSourceDTOMap extends PropertyMap<Thing, ThingDTO> { @Override protected void configure(){ using(new ListThingToThingDTOConverter()).map(source.getThings()).setThings(null); }
最后,您必须将 SourceToSourceDTOMap 添加到您的 ModelMapper,如下所示:
modelMapper = new ModelMapper(); modelMapper.addMappings(new SourceToSourceDTOMap());
您可以通过创建泛型来像下面的代码那样进行映射。 link供参考
http://modelmapper.org/user-manual/generics/
进口:
import java.lang.reflect.Type;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
在您的服务或控制器中class:
ModelMapper modelMapper = new ModelMapper();
Type listType = new TypeToken<SourceDTO>(){}.getType();
SourceDTO sourceDTO = modelMapper.map(source,listType);