You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring整合Solr项目启动报错:UnsatisfiedDependencyException排查

Spring Boot + Solr 启动异常:Required identifier property not found

嘿,我看你在开发Spring Boot集成Solr的应用时遇到了启动异常,错误提示核心是:

java.lang.IllegalStateException: Required identifier property not found for class com.example.webtool.model.Document!

咱们来拆解下问题:

问题原因

Spring Data Solr的SolrCrudRepository是基于CrudRepository实现的,它要求你的实体类(这里是Document)必须包含一个唯一标识字段,对应Solr索引中的唯一键(通常默认是id字段)。因为诸如findById()、save()这类基础CRUD操作都依赖这个唯一标识来定位文档,你的Document类里完全没定义这个字段,所以Spring容器初始化Repository的时候就报错了。

另外还有个小问题:你的DocumentRepository接口上标注了@Service注解,这是多余的——Spring Data会自动为Repository接口生成代理实现类,不需要额外加Service注解,反而可能造成混淆。

解决方案

  1. 在Document类中添加唯一标识字段,对应Solr的id字段,并用@Field注解标记
  2. 移除DocumentRepository上的@Service注解

修改后的代码如下:

修改后的Document类

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 {
    // 新增唯一标识字段,对应Solr的id字段
    @Field("id")
    private String id;
    
    @Field
    private String person;
    @Field
    private String rank;
    @Field
    private String text;

    public Document() { }

    // 构造方法可以按需添加id参数
    public Document(String id, String person, String rank, String text) {
        this.id = id;
        this.person = person;
        this.rank = rank;
        this.text = text;
    }

    // 新增id的getter和setter
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }

    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; }
}

修改后的DocumentRepository类

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

// 移除@Service注解
public interface DocumentRepository extends SolrCrudRepository<Document, String> {
    List<Document> findAllByPerson(String person); 
    List<Document> findAllByRank(String rank); 
}

这样修改后,Spring就能正确识别实体类的唯一标识,Repository也能正常初始化了。

内容的提问来源于stack exchange,提问作者Siddharth Sinha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:49:35