Spring AI实战:快速集成阿里通义千问

发布时间:2026/6/10 19:48:08

Spring AI实战:快速集成阿里通义千问 一、背景AI时代Java开发者如何不掉队最近AI Agent概念火得一塌糊涂ChatGPT、Claude等大模型能写代码、能查资料甚至能调用外部工具。但作为Java后端开发我们最关心的是如何把AI能力集成到现有的Spring Boot系统中官方方案是Spring AI—— 一个专门为Spring开发者设计的AI应用开发框架。它可以让我们像写普通业务代码一样用注解、依赖注入的方式让AI调用Java方法。本文目标用最简洁的代码实现一个能听懂“明天深圳罗湖会下雨吗”这类自然语言并自动调用天气API返回答案的智能助手。二、实现思路核心思路只有三步步骤做什么技术点1️⃣让AI能看懂中文问题接入阿里云通义千问大模型2️⃣告诉AI“你会查天气”用Tool注解标记Java方法3️⃣AI自动决策什么时候该查天气Spring AI框架自动完成一句话原理你只需要写好“查天气”的Java方法用注解说明它的用途AI大模型会自动判断用户问题是否需要调用这个方法并提取参数执行。三、完整实现3步搞定解决使用spring_Ai包3.1目录3.2代码pom.xml?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.3.0/version relativePath/ /parent groupIdcom.dp/groupId artifactIdspring_ai_pro/artifactId version0.0.1-SNAPSHOT/version namespring_ai_pro/name properties java.version17/java.version spring-ai-alibaba.version1.0.0.3/spring-ai-alibaba.version /properties dependencyManagement dependencies dependency groupIdcom.alibaba.cloud.ai/groupId artifactIdspring-ai-alibaba-bom/artifactId version${spring-ai-alibaba.version}/version typepom/type scopeimport/scope /dependency /dependencies /dependencyManagement dependencies !-- 核心DashScope Starter通义千问适配器 -- dependency groupIdcom.alibaba.cloud.ai/groupId artifactIdspring-ai-alibaba-starter/artifactId version1.0.0-M5.1/version /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency /dependencies !-- 需要这两个仓库才能下载依赖 -- repositories repository idspring-milestones/id nameSpring Milestones/name urlhttps://repo.spring.io/milestone/url /repository repository idspring-snapshots/id nameSpring Snapshots/name urlhttps://repo.spring.io/snapshot/url /repository /repositories build plugins plugin groupIdorg.springframework.boot/groupId artifactIdspring-boot-maven-plugin/artifactId /plugin /plugins /build /projectapplication.ymlspring: ai: dashscope: api-key: sk-dc58ae76e4c34020axxxx chat: options: model: qwen-plus server: port: 8080api-key是阿里ai官网申请的API的key,免费。大模型服务平台百炼控制台大模型服务平台百炼控制台百炼控制台是阿里云大模型服务平台提供AI模型训练、部署、推理一站式服务支持多种大模型框架助力企业快速构建AI应用。https://bailian.console.aliyun.com/cn-beijing#/homeWeatherTools.Javapackage com.dp.aipro.tools; import org.springframework.ai.tool.annotation.Tool; import org.springframework.ai.tool.annotation.ToolParam; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; Component // 重要将该类注册为Spring Bean public class WeatherTools { // 模拟一个简单的天气数据库 private static final MapString, String WEATHER_DB new HashMap(); static { WEATHER_DB.put(深圳,明天, 深圳明天多云转晴26-32℃午后可能有短时阵雨。); WEATHER_DB.put(北京,后天, 北京后天晴18-25℃空气质量良。); WEATHER_DB.put(上海,明天, 上海明天小雨22-28℃记得带伞。); // 你可以根据需要添加更多城市... } /** * 核心使用 Tool 注解标记这个方法是一个AI可调用的工具。 * description 是给AI看的“说明书”至关重要 */ Tool(description 根据城市名称和日期获取天气预报。如果用户问明天或后天请先计算出具体日期再传入。) public String getWeather( ToolParam(description 城市名称例如深圳、北京、上海) String city, ToolParam(description 日期格式为YYYY-MM-DD例如2026-06-06) String date) { // 为了演示这里使用简单匹配。实际开发中你可以调用真实的API或查询数据库。 String key city , date; String weatherInfo WEATHER_DB.get(key); if (weatherInfo null) { // 如果找不到精确匹配返回一个默认的模拟数据 return String.format(%s在%s的天气预报多云气温24-30℃。, city, date); } return weatherInfo; } }WeatherController.javapackage com.dp.aipro.controller; import com.dp.aipro.tools.WeatherTools; import org.springframework.ai.chat.client.ChatClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.time.LocalDate; RestController public class WeatherController { private final ChatClient chatClient; Autowired public WeatherController(ChatClient.Builder builder) { // 直接构建 ChatClient不再需要配置 defaultFunctions this.chatClient builder.build(); } GetMapping(/ai/weather) public String askWeather(RequestParam String question, WeatherTools weatherTools) { // 获取当前日期让AI知道“明天”是哪一天 String currentDate LocalDate.now().toString(); String fullQuestion String.format(今天是%s。问题%s, currentDate, question); // 关键在每次请求时通过 .tools() 方法注册工具 return chatClient.prompt(fullQuestion) .tools(weatherTools) // --- 就在这里直接注册工具实例 .call() .content(); } }AiController.javapackage com.dp.aipro.controller; import org.springframework.ai.chat.client.ChatClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; RestController RequestMapping(/ai) public class AiController { private final ChatClient chatClient; public AiController(ChatClient.Builder builder) { this.chatClient builder.build(); } RequestMapping(/chat) public String chat(RequestParam String question) { return this.chatClient.prompt().user(question).call().content(); } }四 测试http://localhost:8080/ai/weather?question%E6%98%8E%E5%A4%A9%E6%B7%B1%E5%9C%B3%E7%BD%97%E6%B9%96%E5%A4%A9%E6%B0%94%E4%BC%9A%E4%B8%8B%E9%9B%A8%E5%90%97http://localhost:8080/ai/chat?question%E6%98%8E%E5%A4%A9%E6%B7%B1%E5%9C%B3%E7%BD%97%E6%B9%96%E5%A4%A9%E6%B0%94%E4%BC%9A%E4%B8%8B%E9%9B%A8%E5%90%97Done.五、原理解析一张图看懂text用户提问明天深圳会下雨吗 ↓ Spring AI 框架 ↓ 发送给通义千问大模型 ↓ 模型理解需要查天气参数{city深圳, date2026-06-10} ↓ 调用 WeatherTools.getWeather() ← 你写的Java方法 ↓ 获取天气数据多云转晴26-32℃ ↓ 大模型组织自然语言回答 ↓ 返回用户深圳明天多云转晴26-32℃...核心价值你不需要写任何“判断用户意图”的代码AI大模型自动完成六、进阶扩展建议扩展方向实现方式接入真实天气API在getWeather()中调用高德/和风天气API添加更多工具新建类用Tool注解方法并在控制器中.tools()注册支持流式输出使用ChatClient的stream()方法添加对话记忆配置ChatMemory支持多轮对话更换其他模型修改application.yml中的model参数七、常见问题Q1依赖下载失败A检查是否配置了Spring里程碑仓库见pom.xml中的repositories。Q2API Key无效A去阿里云百炼控制台确认Key是否正确新用户有免费额度。Q3AI不调用我的工具A检查Tool的description是否清晰给AI的提示词是否包含工具功能说明。Q4支持其他大模型吗ASpring AI支持OpenAI、Azure OpenAI、通义千问、智谱等只需更换starter即可。八、源码获取完整代码已上传lvan-jone · GitHub九、总结本文带你从零实现了一个“能听懂人话、能查天气”的AI智能助手核心代码不超过50行。你只需要✅ 引入Spring AI依赖✅ 配置API Key✅ 用Tool注解标记Java方法✅ 在Controller中用.tools()注册剩下的——意图识别、参数提取、工具调用——全部交给AI大模型如果本文对你有帮助欢迎点赞、收藏、评论三连版权声明本文为CSDN博主「總鑽風」的原创文章遵循CC 4.0 BY-SA版权协议转载请附上原文出处链接及本声明。原文链接Spring AI实战快速集成阿里通义千问-CSDN博客

相关新闻