1

我正在开发 Spring 应用程序,对于对象映射,我正在使用 ModelMapper 库。

我能够映射基本类映射,但是当我尝试映射 2 个集合元素时,源是一组带有附加属性(如名称和描述)的枚举,而目标是具有 id、名称和描述的 pojo。

我已经在映射配置文件中尝试过 typemap 和转换器,但我遇到了 mapper 的例外。

并且源类来自其他应用程序(其依赖项已添加到 pom.xml 中)。我也不希望源类型作为目标设置器中的参数。

前任。

资源:

public class VType{

   private int id;
   private String name;
   private String description;

}

public class VDTO{

   private Set<VType> vTypes;

   public Set<VType> getVTypes(){
       return this.vTypes;
   }

   public void setVType() { //here I don't want to pass source type as an argument
     //code stuff that I don't know what to do here
   }
}

源枚举:

public enum SourceVType{
   V1(1, "Name1", "Desc1");
   V2(2, "Name2", "Desc2");


   private Integer id;
   private String name;
   private String description;


   SourceVType(Integer id, String name, String description) {
       this.id = id;
       this.name = name;
       this.description = description;
   }

   //getter-setter
}
4

1 回答 1

1

您是否尝试过modelmapper. 您可以使用typemap转换器来实现此要求。

@RunWith(JUnit4.class)
public class TempTest {

    @Test
    public void TestThis(){

        final ModelMapper mapper = new ModelMapper();
        mapper.addMappings(new PropertyMap<SrcClass, DestClass>() {
            @Override
            protected void configure() {
                this.map().setId(this.source.getId());
                this.map().setName(this.source.getName());
                mapper.createTypeMap(TypeEnum.class, TypeClass.class).setConverter(
                        new Converter<TypeEnum, TypeClass>() {
                            @Override
                            public TypeClass convert(MappingContext<TypeEnum, TypeClass> mappingContext) {
                                if (mappingContext.getSource() == null) {
                                    return null;
                                }
                                TypeEnum typeEnum = mappingContext.getSource();

                                TypeClass typeClass = new TypeClass();
                                typeClass.setId(typeEnum.getId());
                                typeClass.setName(typeEnum.getName());
                                return typeClass;
                            }
                        });

            }
        });

        SrcClass srcObj = new SrcClass();
        srcObj.setId(1);
        srcObj.setName("name");
        srcObj.setTypes(new HashSet<>(Arrays.asList(TypeEnum.TYPE1, TypeEnum.TYPE2)));

        DestClass dstObj = mapper.map(srcObj, DestClass.class);

        Assert.assertEquals(srcObj.getId(), dstObj.getId());
        Assert.assertEquals(srcObj.getName(), dstObj.getName());
        Assert.assertEquals(srcObj.getTypes().size(), dstObj.getTypes().size());

        for(TypeClass c : dstObj.getTypes()) {
            TypeEnum e = TypeEnum.getById(c.getId());
            Assert.assertNotNull(e);
            Assert.assertTrue(srcObj.getTypes().contains(e));
        }
    }

    public static <Source, Result> Set<Result> convertAll(Set<Source> source, Function<Source, Result> projection)
    {
        Set<Result> results = new HashSet<>();

        if(source == null) return results;

        for (Source element : source)
        {
            results.add(projection.apply(element));
        }

        return results;
    }

    public static class SrcClass{
        private Integer id;
        private String name;

        private Set<TypeEnum> types;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Set<TypeEnum> getTypes() {
            return types;
        }

        public void setTypes(Set<TypeEnum> types) {
            this.types = types;
        }
    }

    public static class DestClass{
        private Integer id;
        private String name;

        private Set<TypeClass> types;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Set<TypeClass> getTypes() {
            return types;
        }

        public void setTypes(Set<TypeClass> types) {
            this.types = types;
        }
    }

    public static enum TypeEnum{

        TYPE1(1, "Type 1")
        , TYPE2(2, "Type 2")
        , TYPE3(3, "Type 3")
        , TYPE4(4, "Type 4");

        private Integer id;
        private String name;

        TypeEnum(Integer id, String name) {
            this.id = id;
            this.name = name;
        }


        private static final Map<Integer, TypeEnum> byId = new HashMap<>();
        private static final Map<String, TypeEnum> byName = new HashMap<>();

        static {
            for (TypeEnum e : TypeEnum.values()) {
                if (byId.put(e.getId(), e) != null) {
                    throw new IllegalArgumentException("duplicate id: " + e.getId());
                }

                if (byName.put(e.getName(), e) != null) {
                    throw new IllegalArgumentException("duplicate name: " + e.getName());
                }
            }
        }

        public Integer getId() {
            return this.id;
        }

        public String getName() { return this.name; }

        public static TypeEnum getById(Integer id) {
            return byId.get(id);
        }

        public static TypeEnum getByName(String name) {
            return byName.get(name);
        }
    }

    public static class TypeClass{
        private Integer id;
        private String name;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
于 2015-05-21T04:13:48.963 回答