org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为'solrDocumentController'的bean时出错

时间:2019-05-30 14:09:34

标签: java spring spring-boot solr

我通过遵循此tutorial

来创建spring应用程序

这是我的主班

package com.example.webtool;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;


@SpringBootApplication
@EnableSolrRepositories(
        basePackages = "com.example.webtool.repository"
)
public class WebtoolApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebtoolApplication.class, args);
    }

}

这是我的存储库类

package com.example.webtool.repository;

import java.util.List;
import org.springframework.data.solr.repository.SolrCrudRepository;
import com.example.webtool.model.Document;
import org.springframework.stereotype.Service;

public interface DocumentRepository extends SolrCrudRepository<Document, String> {
    List<Document> findAllByPerson(String person); // find documents whose person name matches the name of the person in the query
    List<Document> findAllByRank(String rank); // find documents whose rank matches the rank of the person in the query

}

这是我的文档类

package com.example.webtool.model;

import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.solr.core.mapping.SolrDocument;

@SolrDocument(solrCoreName = "gettingstarted")
public class Document {

    @Field
    private String person;

    @Field
    private String rank;

    @Field
    private String text;

    public Document() { }

    public Document(String person, String rank, String text) {
        this.person = person;
        this.rank = rank;
        this.text = text;

    }

    public String getPerson() {
        return person;
    }

    public void setPerson(String person) {
        this.person = person;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

这是我的控制器类

package com.example.webtool.controller;

import com.example.webtool.model.Document;
import com.example.webtool.repository.DocumentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Siddharth Sinha
 */

@RestController
public class SolrDocumentController {

    @Autowired
    private DocumentRepository documentRepository;

    @RequestMapping("/")
    public String SampleController() {
        return "THis is a sample page!!!!";
    }

    @PostMapping("/search")
    public List<Document> getDocs(@RequestBody String name) {
        return this.documentRepository.findAllByPerson(name);
    }
}

当我尝试构建应用程序时,出现如下异常:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'solrDocumentController': Unsatisfied dependency expressed through field 'documentRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'documentRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.solr.repository.support.SimpleSolrRepository]: Constructor threw exception; nested exception is java.lang.IllegalStateException: Required identifier property not found for class com.example.webtool.model.Document!
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]

我的应用程序怎么了?

1 个答案:

答案 0 :(得分:2)

错误日志告诉您:

Required identifier property not found for class com.example.webtool.model.Document!

@Id
@Indexed(name = "id", type = "string")
private String id;