【问题标题】:List attribute to Object attribute - ModelMapper将属性列表到对象属性 - ModelMapper
【发布时间】:2019-04-11 22:37:50
【问题描述】:

我在尝试使用 Java 8 中的模型映射器时遇到了一些问题。 我有一个对象“Person”和一个对象“Documents” 我有类似的情况:

public class Doc   {

  private Integer type;
  private List<Documento> documentos = null;
  private Boolean flag;
}

public class Document {

    private Long doc1;
    private Long doc2;
    private Long doc3;
}

public class Person {

    private Integer type;
    private Long doc1;
    private Long doc2;
    private Long doc3;
    private Boolean flag;
}

modelMapper.addMappings(new PropertyMap<Person, Doc>() { 
    @Override
    protected void configure() {                
        map().setType(source.getType());
        map().setDoc1(source.getDocument().get(0).getDoc1().longValue());
        map().setDoc2(source.getDocument().get(0).getDoc2().longValue());
        map().setDoc3(source.getDocument().get(0).getDoc3().longValue());
        map()setFlag(source.getFlag());
    }
});

但是,这行不通。

源方法 java.util.List.get() 无效。确保该方法的参数为零且不返回 void。

我只需要文档列表的第一个对象。

我该如何解决这个问题?

【问题讨论】:

    标签: java converters modelmapper


    【解决方案1】:

    Hugues M.查看此解决方案:ModelMapper: Ensure that method has zero parameters and does not return void,它回答了涉及您遇到的异常的类似问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      • 2021-11-11
      相关资源
      最近更新 更多