为什么有必要为要与Solr Response绑定的对象创建非参数构造函数?

问题描述 投票:0回答:1

我在我的项目中使用solrj并遵循this教程,我创建了一个对象类

public class Student {
    @Field String id;

    public Student(String studentId) {
        this.id = studentId;
    }

    //setter and getter methods here

}

并使用以下代码从Solr检索数据并将响应绑定到Student对象

final SolrClient client = getSolrClient();
final SolrQuery query = new SolrQuery("*:*");
query.addField("id");

final QueryResponse response = client.query("collection-name", query);
final List<Student> students = response.getBeans(Student.class); 

我在最后一行收到错误。 org.apache.solr.client.solrj.beans.BindingException: Could not instantiate object of class Student

但是,当我将public Student() { }构造函数添加到Student类时,它工作正常。我的问题是,为什么它不能在第一种情况下工作或为什么有必要在这里创建一个非参数构造函数?

java spring-boot data-binding solr solrj
1个回答
0
投票

在所有solrj ref指南中,有一个示例,它有空构造函数,但没有描述。我认为api在获取数据之前调用空构造函数并逐个填充字段。

© www.soinside.com 2019 - 2024. All rights reserved.