文章目录前提maven依赖创建OpenAiController代码postman发送请求推荐使用bom机制完整pom.xmlopenAi配置openAi-百炼配置openAi-deepseek配置springAI对rag的支持实现SSE流式报错报错 cannot retry due to server authentication, in streaming mode做ai当然是python好但是springAI支持了也熟悉下。如果之前做传统项目一般都用java8。springai可不是加个包这么简单整体环境都要换jdk要用17springboot要用3.x相当于整体版本变了。前提Spring Boot 3.x # 例如3.3.5java17环境变量设置好例如用阿里百炼的openAi。DASHSCOPE_API_KEY具体的apikey值# python用这个名字AI_DASHSCOPE_API_KEY具体的apikey值# java用这个名字前面多了AI_maven依赖dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-openai-spring-boot-starter/artifactIdversion1.0.0-M6/version!-- 版本号请根据实际仓库调整建议使用最新 Milestone --/dependency完整pom.xml见下文。创建OpenAiController代码package com.example.demo.web;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.http.ResponseEntity;importorg.springframework.web.bind.annotation.*;importjava.util.HashMap;importjava.util.Map;RestControllerRequestMapping(/openai)publicclassOpenAiController{private final ChatClient chatClient;public OpenAiController(ChatClient.Builder chatClientBuilder){this.chatClientchatClientBuilder.build();}PostMapping(/chat)public ResponseEntityStringchat(RequestBody MapString,Stringrequest){try{String messagerequest.getOrDefault(message,你好);String answerchatClient.prompt().user(message).call().content();returnResponseEntity.ok(answer);}catch(Exception e){returnResponseEntity.internalServerError().body(Error: e.getMessage());}}GetMapping(/chat)public ResponseEntityStringchatGet(RequestParam String message){try{String answerchatClient.prompt().user(message).call().content();returnResponseEntity.ok(answer);}catch(Exception e){returnResponseEntity.internalServerError().body(Error: e.getMessage());}}}postman发送请求请求地址{{url}}/api/openai/chat请求报文{message:今天是几号}返回报文今天的具体日期我这里没办法直接查看因为我没有实时访问系统日期的功能。你可以通过查看设备右下角的任务栏、使用智能助手或者直接搜索“今天的日期”来获取准确的信息。如果你告诉我你所在的时区或地区我可以帮助你理解如何更好地查找这个信息。这回答。。。不过也算调试通了。推荐使用bom机制添加版本号properties!-- 添加如下内容 --spring.ai.version2021.0.5.0/spring.ai.version/properties添加bom管理dependencyManagementdependencies!-- 如果之前已有dependencyManagement标签只添加这段内容 --dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-bom/artifactIdversion${spring.ai.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagement作用springAi的其他相关依赖不用写版本号了统一受bom管理。完整pom.xml拿来就能用方便可以作为一套boot3.x的方案。注springai是通过bom管理的见本文。projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersion!-- 1. 基础 Spring Boot 父工程 (推荐 3.4.5) --parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.3.5/versionrelativePath//parentgroupIdcom.example/groupIdartifactIdspring-ai-demo/artifactIdversion1.0.0-SNAPSHOT/versionnamespring-ai-demo/namepropertiesjava.version17/java.version!-- Spring Boot 3.x 强制要求 JDK 17 --spring-ai.version1.1.2/spring-ai.version/properties!-- 2. 统一版本管理 (BOM) --dependencyManagementdependenciesdependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-bom/artifactIdversion${spring-ai.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagement!-- 3. 核心依赖 --dependencies!-- Spring Boot Web (用于暴露 REST API) --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Spring AI OpenAI Starter (以 OpenAI 为例可替换为其他模型) --dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-starter-model-openai/artifactId/dependency!-- 测试依赖 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.33/versionscoperuntime/scope/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-spring-boot3-starter/artifactIdversion3.5.9/version/dependencydependencygroupIdcom.baomidou/groupIdartifactIddynamic-datasource-spring-boot3-starter/artifactIdversion4.3.1/version/dependencydependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.11.0/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.83/version/dependencydependencygroupIdorg.apache.commons/groupIdartifactIdcommons-lang3/artifactId/dependency/dependencies!-- 4. 必须配置的仓库 (Spring AI 1.0.0 以上已发布至 Maven Central但建议保留以防需要快照) --repositoriesrepositoryidspring-milestones/idnameSpring Milestones/nameurlhttps://repo.spring.io/milestone/urlsnapshotsenabledfalse/enabled/snapshots/repository/repositoriesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/projectopenAi配置是统一的配置形式只用改这几个参数即可ase-url、api-key 和 modelopenAi-百炼配置spring:ai:openai:api-key:${AI_DASHSCOPE_API_KEY}# 读取你配置好的环境变量base-url:https://dashscope.aliyuncs.com/compatible-mode# 百炼兼容模式地址chat:options:model:qwen-max# 或 qwen-plusopenAi-deepseek配置spring:ai:openai:api-key:${DEEPSEEK_API_KEY}# 读取你配置好的环境变量base-url:https://api.deepseek.com/v1# 百炼兼容模式地址chat:options:model:deepseek-chat# 或 deepseek-reasonerspringAI对rag的支持见rag笔记。实现SSE流式非常简单已经封装好了。/** * 流式对话接口 * 前端通过 EventSource 或 fetchReadableStream 消费 */PostMapping(value/chat/stream,producesMediaType.TEXT_EVENT_STREAM_VALUE)publicFluxStringstreamChat(RequestBodyMapString,Stringrequest){Stringmessagerequest.getOrDefault(message,你好);returnchatClient.prompt().user(message).stream()// 关键把 .call() 换成 .stream().content();// 返回 FluxString逐 token 发射}用postman等post工具就能测见sse笔记。报错报错 cannot retry due to server authentication, in streaming mode提示授权一般是apikey的问题。1、环境变量是否已设置apikey如果是报错后才设置的需要重启idea直接在yaml中写apikey值不用重启idea因为直接赋值不涉及读取环境变量2、yaml中有没有引入3、apikey值是否正确实测自己遇到的是没有设置环境变量后来设置了也没重启还是报错重启后问题解决。