简体   繁体   English

Java Mapstruct 通用枚举转换器

[英]Java Mapstruct generic enum converter

I have two different enum classes and one of them use them as a value like我有两个不同的枚举类,其中一个将它们用作一个值

public enum Type{

    TEST1{
        @Override
        public Type convertToINT() {
            return Type.INTEGRATION1;
        }
    },
    TEST2{
        @Override
        public Type convertToINT() {
            return Type.INTEGRATION2;
        }
    },
    TEST3{
        @Override
        public TypeIntegration convertToINT(){
            return Type.INTEGRATION3;
        }
    };

   public abstract TypeIntegration convertToINT();
}


public enum TypeIntegration {

    INTEGRATION1,
    INTEGRATION2,
    INTEGRATION3
}

This enums uses in different classes for example ;例如,这个枚举在不同的类中使用;

@Getter
@Setter
public class typeSaveReqDto{
 private blabla;
 private blabla;
 private Type type;
}


@Getter
@Setter
public class typeIntegrationObject{
 private blabla;
 private blabla
 private TypeIntegration type;
}

I want to use mapstructs and convert via auto generated classes, but mapstruct throws me exception like "The following constants from the property "Type type" enum have no corresponding constant in the "TypeIntegration type" enum and must be be mapped via adding additional mappings: TEST1, TEST2, TEST3"我想使用 mapstructs 并通过自动生成的类进行转换,但 mapstruct 向我抛出异常,例如“属性“类型类型”枚举中的以下常量在“类型集成类型”枚举中没有对应的常量,必须通过添加附加映射来映射:TEST1、TEST2、TEST3"

I want to create EnumMapper classes for converting enums, How can i write generic classes for this with mapStructs java ?我想创建用于转换枚举的 EnumMapper 类,如何使用 mapStructs java 编写通用类?

Edit :编辑 :

I generated EnumMapper我生成了 EnumMapper

@Mapper
public class EnumMapper {

    EnumMapper INSTANCE = Mappers.getMapper(EnumMapper.class);

    @Named("enumToIntEnum")
    public static <T extends EnumConverter<INT>,INT> INT convertToINT(T enums, @TargetType INT enumClass){
        INT convertObj = ((T)enums).convertToINT();
        return convertObj;
    }
}

And Mapper interfaces like below和 Mapper 接口如下

@Mapper(componentModel = "spring",uses = EnumMapper.class)
public interface TypeReqMapper {

    
        @Mapping(source = "type" , target = "type",qualifiedByName = "enumToIntEnum")
    public TypeIntegrationObject typeSaveReqDtoTotypeIntegrationObject(TypeSaveReqDto typeSaveReqDto);

}

But I got a fail like 'Can't map property "Type type" to "TypeIntegration type".但是我遇到了一个失败,比如“无法将属性“类型类型”映射到“类型集成类型”。 Consider to declare/implement a mapping method: "TypeIntegration map(Type value)".'考虑声明/实现一个映射方法:“TypeIntegration map(Type value)”。

Type T isn't know and doesn't contain a convertToINT method as it can be anything.类型T未知且不包含convertToINT方法,因为它可以是任何东西。 So keep it simple and just write a dedicated mapper instead of trying to build one that does everything.所以保持简单,只写一个专用的映射器,而不是尝试构建一个可以做所有事情的映射器。

@Mapper(componentModel="spring")
public class TypeToTypeIntegrationMapper {

  @Mapping
  public TypeIntegration map(Type from) {
    return from.convertToINT();
  }
}

Don't make it more complex.不要让它变得更复杂。

You could even ditch the mapper and write an expression instead.您甚至可以放弃映射器并改为编写expression

@Mapper(componentModel = "spring",uses = EnumMapper.class)
public interface TypeReqMapper {

    
        @Mapping(source = "type" , target = "type", expression = "java(type.convertToINT())")
    public TypeIntegrationObject typeSaveReqDtoTotypeIntegrationObject(TypeSaveReqDto typeSaveReqDto);

}

MapStruct will now use the expression to generate the mapping code instead of you having to write a mapper yourself. MapStruct 现在将使用表达式来生成映射代码,而不必自己编写映射器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM