And*_*sky 5 java enums mapstruct
如何使用 mapstruct 将 enum 转换为 POJO 而无需自定义实现?
例如
enum Type {
T1, T2;
private String description;
private Type(String description) {
this.description = description;
}
public String getDescription() { return this.description; }
}
Run Code Online (Sandbox Code Playgroud)
POJO之类的
class TypeDto {
private Type code;
private String description;
}
Run Code Online (Sandbox Code Playgroud)
仅供参考,我使用 MapStruct 1.1.0.Final。
您不能直接从枚举转换为对象。
您需要创建一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
public class TypeMapperImpl implements TypeMapper {
@Override
public TypeDto typeToTypeDto(Type type) {
if (type == null) {
return null;
}
return new TypeDto(type.getDescription());
}
}
Run Code Online (Sandbox Code Playgroud)
您可以通过创建通用映射器来使该映射器可重用。
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@Mapper
public interface EnumMapper<T, U extends Enum<?>> {
@Mapping(target = "description")
T enumToObject(U type);
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
然后您可以在 TypeMapper 中使用它。
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface TypeMapper extends EnumMapper<TypeDto, Type> {
TypeMapper INSTANCE = Mappers.getMapper(TypeMapper.class);
}
Run Code Online (Sandbox Code Playgroud)
public class TypeMapperImpl extends EnumMapperImpl<TypeDto, Type> implements TypeMapper {
@Override
protected TypeDto convert(Type type) {
return new TypeDto(type.getDescription());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11601 次 |
| 最近记录: |