Enums 模型映射器未映射

Enums 模型映射器未映射,enums,modelmapper,Enums,Modelmapper,当我试图通过枚举将源中的字符串映射到目标中的整数时。ModelMapper失败 来源 public class Request { private String classification; } 目的地 public class DTO { private Integer classification; } 字符串和整数之间的映射在枚举中定义 public enum Classification { POWER(3, "Power"), PERFORMANCE(4, "P

当我试图通过枚举将源中的字符串映射到目标中的整数时。ModelMapper失败

来源

public class Request {
    private String classification;
}
目的地

public class DTO {
    private Integer classification;
}
字符串和整数之间的映射在枚举中定义

public enum Classification {

POWER(3, "Power"),
PERFORMANCE(4, "Performance"),
TASK(13, "Task");

private final Integer code;
private final String  name;

ProblemClassification(final int code, final String name) {
    this.code = code;
    this.name = name;
}

public Integer getCode() {
    return code;
}

public String getName() {
    return name;
}

public static Integer getCodeByName(String name) {
    Optional<Classification> classification = Arrays.asList(Classification.values()).stream()
            .filter(item -> item.getName().equalsIgnoreCase(name))
            .findFirst();
    return classification.isPresent() ? classification.get().getCode() : null;
}
}
公共枚举分类{
权力(3,“权力”),
业绩(4,“业绩”),
任务(13,“任务”);
私有最终整数码;
私有最终字符串名;
ProblemClassification(最终整数代码、最终字符串名称){
this.code=代码;
this.name=名称;
}
公共整数getCode(){
返回码;
}
公共字符串getName(){
返回名称;
}
公共静态整数getCodeByName(字符串名称){
可选分类=Arrays.asList(classification.values()).stream()
.filter(项目->项目.getName().equalsIgnoreCase(名称))
.findFirst();
返回分类.isPresent()?分类.get().getCode():null;
}
}

您需要
转换器
在那里:

ModelMapper modelMapper = new ModelMapper();
Converter<String, Integer> classificationConverter =
                ctx -> ctx.getSource() == null ? null : Classification.getCodeByName(ctx.getSource());
modelMapper.typeMap(Request.class, DTO.class)
                .addMappings(mapper -> mapper.using(classificationConverter).map(Request::getClassification, DTO::setClassification));
ModelMapper ModelMapper=newmodelmapper();
转换器分类转换器=
ctx->ctx.getSource()==null?null:Classification.getCodeByName(ctx.getSource());
modelMapper.typeMap(Request.class,DTO.class)
.addMappings(mapper->mapper.using(classificationConverter).map(Request::getClassification,DTO::setClassification));

您需要
转换器
在那里:

ModelMapper modelMapper = new ModelMapper();
Converter<String, Integer> classificationConverter =
                ctx -> ctx.getSource() == null ? null : Classification.getCodeByName(ctx.getSource());
modelMapper.typeMap(Request.class, DTO.class)
                .addMappings(mapper -> mapper.using(classificationConverter).map(Request::getClassification, DTO::setClassification));
ModelMapper ModelMapper=newmodelmapper();
转换器分类转换器=
ctx->ctx.getSource()==null?null:Classification.getCodeByName(ctx.getSource());
modelMapper.typeMap(Request.class,DTO.class)
.addMappings(mapper->mapper.using(classificationConverter).map(Request::getClassification,DTO::setClassification));

谢谢,我编写了这个转换器,它可以工作,但问题是,有两个类似的字段。第二个字段是Priority,如果我将Priority映射到另一个枚举。这也使用了相同的转换器,映射没有正确发生。@Jags,为优先级枚举定义一个新的
转换器
。谢谢,由于某种原因,它没有按预期工作,但现在可以工作了。刚刚在git中上传了这个简单的项目,所以这可能会帮助其他人。谢谢,我写了这个转换器,它可以工作,但问题是,有两个类似的领域。第二个字段是Priority,如果我将Priority映射到另一个枚举。这也使用了相同的转换器,映射没有正确发生。@Jags,为优先级枚举定义一个新的
转换器
。谢谢,由于某种原因,它没有按预期工作,但现在可以工作了。刚刚在git中上传了这个简单的项目,所以这可能会帮助其他人。