Spring AI 1.0.0 + ChromaDB 最新版踩坑:Collection does not exist 404 报错全记录

报错信息

复制代码
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'chromaVectorStore':
Collection [goods_info] does not exist

Caused by: java.lang.RuntimeException: Collection [goods_info] does not exist
    at org.springframework.ai.chroma.vectorstore.ChromaApi.getCollection(ChromaApi.java:235)
    at org.springframework.ai.chroma.vectorstore.ChromaVectorStore.afterPropertiesSet(ChromaVectorStore.java:121)

Caused by: org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found:
    "{"error":"NotFoundError","message":"Collection [goods_info] does not exist"}"

环境信息

  • Spring AI BOM:1.0.0
  • ChromaDB:最新版(docker chromadb/chroma:latest
  • 依赖:spring-ai-starter-vector-store-chroma

问题根因

根因一:Spring AI 1.0.0 源码中有一个字符串比对 bug

ChromaApi.getCollection 方法里,捕获异常时做了如下判断:

java 复制代码
if (String.format("Collection [%s] does not exists", collectionName).equals(msg)) {
    return null;
}

注意这里是 does not exists(语法错误,多了一个 s)。

而 ChromaDB 最新版实际返回的错误消息是:

复制代码
Collection [goods_info] does not exist

(没有 s)

两个字符串不匹配,导致异常没有被正确捕获,直接向上抛出 RuntimeException,应用启动失败。

根因二:afterPropertiesSet 强制执行检查

ChromaVectorStore 实现了 InitializingBean,Spring 在 Bean 初始化完成后会自动调用 afterPropertiesSet(),其内部强制调用了 getCollection() 做存在性校验,无论 initializeSchema 设置为 true 还是 false 都会执行,无法跳过。

这意味着即使 collection 确实存在,只要版本不匹配导致错误消息字符串对不上,就会一直报错。


排查过程

  1. 首先怀疑 host 配置缺少 http:// 协议头 → 修复后报新错误
  2. 怀疑 collection 不存在 → curl 手动创建,发现代理拦截导致请求没发出去
  3. 绕过代理 --noproxy '*' 后确认 collection 已创建(409 Conflict)
  4. 降级 Chroma 到 0.5.23 → API 路径从 v1 变成 v2,请求格式又不兼容
  5. 尝试反射绕过 afterPropertiesSetbuild() 内部就触发了,时机来不及
  6. 最终定位到源码字符串 bug → 升级 Spring AI BOM 到 1.1.4 彻底解决

解决方案

✅ 最终方案:升级 Spring AI BOM 到 1.1.4

xml 复制代码
<spring-ai.version>1.1.4</spring-ai.version>

1.1.x 版本修复了该字符串比对 bug,initializeSchema(true) 会正确自动创建 collection。

配置文件(application.properties)

properties 复制代码
# OpenAI 大模型
spring.ai.openai.api-key=your-api-key
spring.ai.openai.base-url=https://api.openai.com
spring.ai.openai.chat.options.model=gpt-4o
spring.ai.openai.chat.options.temperature=0.7
spring.ai.openai.embedding.options.model=text-embedding-ada-002

# Chroma 向量数据库
spring.ai.vectorstore.chroma.client.host=http://192.168.0.x
spring.ai.vectorstore.chroma.client.port=8321
spring.ai.vectorstore.chroma.tenant-name=default_tenant
spring.ai.vectorstore.chroma.database-name=default_database

配置类(ChromaConfig.java)

java 复制代码
@Configuration
public class ChromaConfig {

    @Bean
    public ChromaVectorStore chromaVectorStore(ChromaApi chromaApi, EmbeddingModel embeddingModel) {
        return ChromaVectorStore.builder(chromaApi, embeddingModel)
                .collectionName("goods_info")
                .initializeSchema(true)
                .build();
    }
}

Docker 启动 ChromaDB

bash 复制代码
docker run -d \
  --name chroma \
  -p 8321:8000 \
  -v /your/data/path:/chroma/chroma \
  chromadb/chroma:latest

注意事项

  • host 必须带 http:// 协议头,不能只写 IP
  • 如果服务器上有 http_proxy 环境变量,curl 请求会走代理,需要加 --noproxy '*' 绕过
  • tenantdatabase 使用 default_tenant / default_database 开箱即用,无需手动创建
  • collection 相当于数据库中的表,initializeSchema(true) 会在启动时自动创建
相关推荐
胡单纯2 小时前
AI 直接解析 PDF 文档!OpenClaw 2026.3.3 新功能实测太强了
数据库·人工智能·pdf
盟接之桥2 小时前
盟接之桥®说制造:从“制造”到“智造”,以品类品牌重塑制造业的生态未来
大数据·网络·人工智能·学习·制造
User_芊芊君子2 小时前
Python+Agent入门实战:0基础搭建可复用AI智能体
开发语言·人工智能·python
迷你可可小生2 小时前
图像视觉面经学习(一)
图像处理·人工智能·python·学习
炼金士2 小时前
大模型、运筹优化、概率论与控制论在港口物流智能调度中的融合应用(挑战与未来研究报告)
人工智能·概率论·集装箱码头
铁蛋AI编程实战2 小时前
旧电脑秒变 AI 员工:OpenClaw 本地部署教程(含环境配置 + 插件开发 + 常见坑)
人工智能
平安的平安2 小时前
用 Python 玩转 AI 绘图:Stable Diffusion 本地部署指南
人工智能·python·stable diffusion
开开心心就好2 小时前
操作简单的ISO文件编辑转换工具
java·前端·科技·edge·pdf·安全威胁分析·ddos
卷卷说风控2 小时前
工作流的 Skill 怎么写?
java·javascript·人工智能·chrome·安全