首页 > 解决方案 > 带有抽象类的推土机映射

问题描述

我正在使用推土机通过注释方法在 bean 和模型之间进行映射。一切都适用于大多数课程。

但是最近当我的 bean 使用 1 个参数保护构造函数时,我遇到了 1 个问题。因为这个构造函数在某些地方使用了super关键字。

下面是示例示例:

TestBean.java:

abstract public class TestBean{

  public enum Type {
    ALL, FIELD, QUERY, HIERARCHY
  }

  public enum Sort {
    COUNT, INDEX, VALUE
  }

  private Type type;
  private String description;
  private Sort sort;
  
  public FacetBean() {
  }

  protected FacetBean(FacetBean from) {
    setId(from.getId());
    description = from.description;
    sort = from.sort;  
  }

  public Type getType() {
    return type;
  }

  public void setType(Type type) {
    this.type = type;
  }

  public String getDescription() {
    return description;
  }

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

  public Sort getSort() {
    return sort;
  }

  public void setSort(Sort sort) {
    this.sort = sort;
  }
}

下面是我的模型的示例代码。

测试模型.java

public class TestModel{

  public enum Type {
    ALL, FIELD, QUERY, HIERARCHY
  }

  public enum Sort {
    COUNT, INDEX, VALUE
  }

  private Type type;
  private String description;
  private Sort sort;
  

  public Type getType() {
    return type;
  }

  public void setType(Type type) {
    this.type = type;
  }

  public String getDescription() {
    return description;
  }

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

  public Sort getSort() {
    return sort;
  }

  public void setSort(Sort sort) {
    this.sort = sort;
  }
}

我正在使用org.dozer.mapper模型和bean之间的映射。

以下是工作代码:

mapper.map(testBean, TestModel.class)-->工作

如果我反过来尝试,那么它给了我 mapper.map(testModel, TestBean.class)->不工作

org.dozer.MappingException: java.lang.InstantiationException

知道如何解决这个问题吗?(我无法更改 TestBean.java,因为它是一个非常古老的类并且在很多地方使用,我最近创建了 TestModel.java 来将 TestBean.java 的数据映射到它并作为 API 响应返回)

谢谢!

标签: javaconstructormappingjavabeansdozer

解决方案


推荐阅读