4

如何在没有自定义实现的情况下使用 mapstruct 将枚举转换为 POJO?

例如

enum Type {
  T1, T2;

  private String description;

  private Type(String description) {
      this.description = description;
  }

  public String getDescription() { return this.description; }
}

像 POJO

class TypeDto {
   private Type code;
   private String description;
}

仅供参考,我使用 MapStruct 1.1.0.Final。

4

3 回答 3

1

我现在用这个

default TypeDto typeToTypeDto(Type type) {
    return new TypeDto(type.name(), type.getName());
}

由于缺乏其他解决方案。

于 2017-04-17T20:07:25.200 回答
1

您不能直接从枚举转换为对象。

您需要创建一个TypeMapper和一个实现来处理转换。

类型转换

public class TypeConversion {
    public static void main(String[] args) {
        TypeDto t1 = TypeMapper.INSTANCE.typeToTypeDto(Type.T1);
        TypeDto t2 = TypeMapper.INSTANCE.typeToTypeDto(Type.T2);

        System.out.println(t1);
        System.out.println(t2);
    }
}

类型

public enum Type {
    T1("T-One"),
    T2("T-Two");

    private final String description;

    private Type(String description) {
        this.description = description;
    }

    public String getDescription() {
        return this.description;
    }
}

类型Dto

public class TypeDto {
    private String description;

    public TypeDto() {
        this("");
    }

    public TypeDto(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return String.format("TypeDto { \"description\": \"%s\" }", description);
    }
}

类型映射器

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

@Mapper
public interface TypeMapper {
    TypeMapper INSTANCE = Mappers.getMapper(TypeMapper.class);

    @Mapping(source = "description", target = "description")
    TypeDto typeToTypeDto(Type type);
 }

TypeMapperImpl

public class TypeMapperImpl implements TypeMapper {
    @Override
    public TypeDto typeToTypeDto(Type type) {
        if (type == null) {
            return null;
        }

        return new TypeDto(type.getDescription());
    }
}

您可以通过创建通用映射器使此映射器可重用。

枚举映射器

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper
public interface EnumMapper<T, U extends Enum<?>> {
    @Mapping(target = "description")
    T enumToObject(U type);
 }

EnumMapperImpl

public abstract class EnumMapperImpl<T, U extends Enum<?>> implements EnumMapper<T, U> {
    @Override
    public T enumToObject(U type) {
        if (type == null) {
            return null;
        }

        return convert(type);
    }

    protected abstract T convert(U type);
}

然后你可以在你的 TypeMapper 中使用它。

类型映射器

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper
public interface TypeMapper extends EnumMapper<TypeDto, Type> {
    TypeMapper INSTANCE = Mappers.getMapper(TypeMapper.class);
}

TypeMapperImpl

public class TypeMapperImpl extends EnumMapperImpl<TypeDto, Type> implements TypeMapper {
    @Override
    protected TypeDto convert(Type type) {
        return new TypeDto(type.getDescription());
    }   
}
于 2016-12-06T16:04:21.800 回答
1

It's not something that MapStruct can automatically handle for you. Just implement the mapping by hand. MapStruct does not aim to handle every mapping case for you, but automate the common 80% and let you deal with the more exotic cases yourself.

于 2016-12-07T21:51:47.607 回答