2017-05-04 62 views
0

2枚舉:Dozer是否允許從Enum轉換爲Enum?

enum Source { 

    WINTER, 
    SPRING, 
    SUMMER, 
    AUTUMN 
} 

enum Dest { 

    WINTER, 
    SPRING, 
    SUMMER, 
    AUTUMN 
} 

試圖Source轉換爲DestDozer

DozerBeanMapper mapper = new DozerBeanMapper(); 

mapper.map(Source.WINTER, Dest.class); 

異常線程 「main」 org.dozer.MappingException: java.lang.NoSuchMethodE xception:Dest。 < init>()

但是當我試圖轉換包含上面提到的枚舉的複雜對象時,Dozer成功地轉換了它。

那麼,爲什麼Dozer不能將Enum轉換爲Enum,因爲它們不是複雜對象的屬性?

回答

1

推土機無法映射枚舉,因爲它依賴於缺省無參數公共構造函數的存在。只有當它們是較大的POJO的一部分時才能映射它們。例如:

enum Source { 

    WINTER, 
    SPRING, 
    SUMMER, 
    AUTUMN 
} 

public class SourceClass{ 

    private Source season; 

    public Source getSeason() { 
    return season; 
    } 

    public void setSeason(Source season) { 
    this.season = season; 
    } 

} 

enum Dest { 

    WINTER, 
    SPRING, 
    SUMMER, 
    AUTUMN 
} 

public class DestClass{ 

    private Dest season; 

    public Dest getSeason() { 
    return season; 
    } 

    public void setSeason(Dest season) { 
    this.season = season; 
    } 

} 

現在,這將工作:

SourceClass source = new SourceClass(); 
source.setSeason(Source.AUTUMN); 

DestClass dest = mapper.map(source, DestClass.class);