2个枚举:

enum Source {

    WINTER,
    SPRING,
    SUMMER,
    AUTUMN
}




enum Dest {

    WINTER,
    SPRING,
    SUMMER,
    AUTUMN
}


尝试使用SourceDest转换为Dozer

DozerBeanMapper mapper = new DozerBeanMapper();

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



  线程“主” org.dozer.MappingException中的异常:
  java.lang.NoSuchMethodException:目标()


但是,当我尝试转换包含上述枚举的复杂对象时,Dozer成功地对其进行了转换。

因此,当Dozer不是复杂对象的属性时,为什么Dozer无法将Enum转换为Enum?

最佳答案

Dozer无法映射枚举,因为它依赖于默认无参数公共构造函数的存在。仅当它们是较大的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);

10-07 15:27