Spring Mongodb-无法实例化bean类[java.util.List]:指定的类是一个接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20667057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:28:09  来源:igfitidea点击:

Spring Mongodb- Could not instantiate bean class [java.util.List]: Specified class is an interface

javamongodbspring-dataspring-data-mongodb

提问by VBJ

my project is using Spring data mongodb. I was not having below error until i made an edit to one of the document that has a field with Array of Documents in it. It was working fine before but now I keep getting the below error.

我的项目正在使用 Spring 数据 mongodb。直到我对其中包含文档数组字段的文档之一进行编辑后,我才遇到以下错误。以前工作正常,但现在我不断收到以下错误。

The field i updated was impapps in the Projects POJO class. I am not sure how to clear this error tried different things but did not work out.

我更新的字段是 Projects POJO 类中的 impapps。我不知道如何清除此错误尝试了不同的方法但没有解决。

    SEVERE: Servlet.service() for servlet [appServlet] in context with path [/mongodproject] threw exception [Request processing failed; nested exception is org.springframework.data.mapping.model.MappingInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface] with root cause
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
    at org.springframework.data.convert.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:60)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:232)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:212)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1008)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.access0(MappingMongoConverter.java:75)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:957)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:713)

Here are my POJO and Spring Repository class.

这是我的 POJO 和 Spring Repository 类。

Project POJO Class

项目 POJO 类

@Document(collection="releases")
 public class Project {
@Id
private String id;
 ......
@Field("impapps")
private List<ImpactedApplications> impapps=new ArrayList<ImpactedApplications>();
    .....getters/setters
}   

ImpactedApplication POJO Class:

ImpactedApplication POJO 类:

public class ImpactedApplications {

@Field("appid")
private String appId;
.....
@Field("repository")
private List<ScriptsRepo> rep=new ArrayList<ScriptsRepo>();
@Field("artifacts")
private List<Artifacts> artifacts=new ArrayList<Artifacts>();
     //getter and setters

}

}

Artifacts POJO Class

工件 POJO 类

public class Artifacts {

    @Field("artifacttype")
    private String artifactType;
    @Field("documentlink")
    private String documentLink;
    @Field("arttestphase")
    private String artTestPhase;
    @Field("artifactname")
    private ArtifactsEnums artifactsNames;
    @Field("startdate")
    private String startDate;
    @Field("enddate")
    private String endDate;
    @Field("peerrev")
    private boolean peerReview;
    @Field("busrev")
    private boolean busReview;
    @Field("na")
    private boolean na;

Spring Repository classes

Spring 存储库类

public interface ProjectRepository extends Repository<Project, String> {

Project findById(String id);
List<Project> findByYearAndReleaseMonthNoOrderByProjectNameDesc(String year,String month, Sort sort);
Project findByYearAndReleaseMonthNoAndId(String year, String month,String id);

Whenever i call the above methods i keep getting the exception.

每当我调用上述方法时,我都会收到异常。

Below is how my document is looking currently.

以下是我的文档目前的外观。

enter image description here

在此处输入图片说明

采纳答案by Oliver Drotbohm

The impappsfield in your document is not an array but a nested document. So if you change your List<ImpactedApplications>to a simple ImpactedApplicationsthis should read fine.

impapps文档中的字段不是数组而是嵌套文档。所以如果你把你的改成List<ImpactedApplications>简单的,ImpactedApplications这应该很好读。

回答by bastien

I have got the same exception :

我有同样的例外:

Try to declare your field like this :

尝试像这样声明您的字段:

@Field("impapps")
ArrayList<ImpactedApplications> impapps = new ArrayList<ImpactedApplications>();

I don't like to do that but it works for me.

我不喜欢那样做,但它对我有用。

edit:

编辑:

My issue was due to unwind operation during aggregation. It transform array (declared as List<> in my class) into object and then reflection doesn't work because spring was expecting a list.

我的问题是由于聚合期间的展开操作。它将数组(在我的类中声明为 List<> )转换为对象,然后反射不起作用,因为 spring 期待一个列表。