java 如何使用modelMapper转换嵌套类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36717365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:48:20  来源:igfitidea点击:

How to use modelMapper to convert nested classes

javamodelmapper

提问by user497087

I have a simple class that I want to map to a DTO class using modelMapper.

我有一个简单的类,我想使用 modelMapper 将其映射到 DTO 类。

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
}

and I want to convert these to a sourceDTO that contains a list of ThingDTOs, for example

我想将这些转换为包含 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
}

If I drop my list of Things and list of ThingsDTO then modelmapper is a delight to use,

如果我删除我的事物列表和事物DTO列表,那么使用模型映射器是一种乐趣,

 modelMapper.map(source, SourceDTO.class);

But I can't work out how to get the mapper to convert the List of Things to List of ThingDTOs. From the documentation, I think I need to create a mapper class that extends PropertyMap but I can't work out how to configure it.

但是我不知道如何让映射器将事物列表转换为事物列表。从文档中,我认为我需要创建一个扩展 PropertyMap 的映射器类,但我不知道如何配置它。

Any pointers to the relevant documentation would be welcome

欢迎任何指向相关文档的指针

采纳答案by Pau

I think if you configure your ModelMapper as LOOSE or STANDARD it will do for you.

我认为如果您将 ModelMapper 配置为 LOOSE 或 STANDARD,它会为您服务。

modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE);

Otherwhise you could try next:

否则你可以尝试下一个:

  1. You may create a converter like:

    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;
      }}
    
  2. Then customize a Mapping Thing to ThingDTO as next:

        public class SourceToSourceDTOMap extends PropertyMap<Thing, ThingDTO> {
              @Override
              protected void configure(){
                   using(new ListThingToThingDTOConverter()).map(source.getThings()).setThings(null);
              }
    
  3. Finally you must add SourceToSourceDTOMap to your ModelMapper as below:

    modelMapper = new ModelMapper();
    modelMapper.addMappings(new SourceToSourceDTOMap());
    
  1. 您可以创建一个转换器,如:

    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;
      }}
    
  2. 然后自定义一个映射事物到 ThingDTO 如下:

        public class SourceToSourceDTOMap extends PropertyMap<Thing, ThingDTO> {
              @Override
              protected void configure(){
                   using(new ListThingToThingDTOConverter()).map(source.getThings()).setThings(null);
              }
    
  3. 最后,您必须将 SourceToSourceDTOMap 添加到您的 ModelMapper 中,如下所示:

    modelMapper = new ModelMapper();
    modelMapper.addMappings(new SourceToSourceDTOMap());
    

回答by Vignesh_A

You can map like the below code by creating generics . link for reference

您可以通过创建泛型来映射如下代码。链接供参考

http://modelmapper.org/user-manual/generics/

http://modelmapper.org/user-manual/generics/

imports :

进口:

import java.lang.reflect.Type;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;

In your service or controller class:

在您的服务或控制器类中:

ModelMapper modelMapper = new ModelMapper();
Type listType = new TypeToken<SourceDTO>(){}.getType();
SourceDTO sourceDTO = modelMapper.map(source,listType);