报错信息
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 确实存在,只要版本不匹配导致错误消息字符串对不上,就会一直报错。
排查过程
- 首先怀疑 host 配置缺少
http://协议头 → 修复后报新错误 - 怀疑 collection 不存在 → curl 手动创建,发现代理拦截导致请求没发出去
- 绕过代理
--noproxy '*'后确认 collection 已创建(409 Conflict) - 降级 Chroma 到 0.5.23 → API 路径从 v1 变成 v2,请求格式又不兼容
- 尝试反射绕过
afterPropertiesSet→build()内部就触发了,时机来不及 - 最终定位到源码字符串 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 '*'绕过 tenant和database使用default_tenant/default_database开箱即用,无需手动创建collection相当于数据库中的表,initializeSchema(true)会在启动时自动创建