java 使用 mapstruct 从字符串到枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48570827/
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 10:06:42  来源:igfitidea点击:

From string to enum using mapstruct

javaenumsmapstruct

提问by m.zemlyanoi

I want to convert String to enum using mapstruct

我想使用 mapstruct 将 String 转换为 enum

enum TestEnum {
   NO("no");
   String code;

   TestEnum(String code) {
     this.code = code
   }

   public String getCode() {
    return code;
   }
}

I have a code that I've got from service and I want to convert this code to Enum how to do it with easier way by mapstruct

我有一个从服务中获得的代码,我想将此代码转换为 Enum 如何通过 mapstruct 以更简单的方式做到这一点

采纳答案by Bertrand Cedric

Here is a solution with an abstract mapper, but if you want you can convert it with a default methode or a class

这是一个带有抽象映射器的解决方案,但如果您愿意,可以使用默认方法或类进行转换

@Mapper
public abstract class TestMapper {

    abstract Source toSource(Target target);
    abstract Target totarget(Source source);

    String toString(TestEnum test){
        return test.getCode();
    }
    TestEnum toEnum(String code){
        for (TestEnum testEnum : TestEnum.values()) {
            if(testEnum.equals(code)){
                return testEnum;
            }
        }
        return null;
    }
}

public class Source {    
    String value;    
    public String getValue() {
        return value;
    }    
    public void setValue(String value) {
        this.value = value;
    }    
}

public class Target {
    TestEnum value;
    public TestEnum getValue() {
        return value;
    }
    public void setValue(TestEnum value) {
        this.value = value;
    }
}

回答by jpCharger

The following code worked for me.

以下代码对我有用。

@Mappings({
        @Mapping(source = "genderDTO.name", target = "genderName")
})
GenderRecord dtoTogenderRecord(GenderDTO genderDTO);
  • "genderName" is the Enum
  • "genderDTO.name" is the String
  • “性别名称”是枚举
  • “genderDTO.name”是字符串

The result was:

结果是:

@Override
public GenderRecord dtoTogenderRecord(GenderDTO genderDTO) {
    if ( genderDTO == null ) {
        return null;
    }

    GenderRecord genderRecord = new GenderRecord();

    if ( genderDTO.getName() != null ) {
        genderRecord.setGenderName( Enum.valueOf( GenderType.class, genderDTO.getName() ) );
    }

    return genderRecord;
}

I also use the following at the interface level to ensure null checks:

我还在接口级别使用以下内容来确保空检查:

@Mapper(nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)

回答by Ryan Augustine

MapStruct only calls the "setProperty(PropertyType)" function, so simply use polymorphism in your setters

MapStruct 只调用“setProperty(PropertyType)”函数,所以只需在你的 setter 中使用多态

For DTO:

对于 DTO:

    public void setStatus(String status) {
        this.status = status;
    }

    public void setStatus(ProjectStatus status) {
        this.status = status.toString();
    }

For Entity:

对于实体:

    public void setStatus(ProjectStatus status) {
        this.status = status;
    }

    public void setStatus(String status) {

        switch (status) {
        case "PENDING_WITH_RM":
            this.status = ProjectStatus.PENDING;
            break;
        case "PENDING_WITH_OPS":
            this.status = ProjectStatus.PENDING;
            break;
        case "COMPLETED":
            this.status = ProjectStatus.COMPLETED;
            break;
        default:
            this.status = ProjectStatus.DRAFT;
        }
    }