一、项目背景与目标本系列旨在通过实战案例掌握 Spring AI 的核心用法。我们将构建一个支持多模型切换的聊天后端并提供前端页面进行交互测试。核心功能点基于 Spring Boot 3.x Spring AI 快速起步集成 Swagger/Knife4j 进行接口调试对接云端大模型OpenAI, DeepSeek, 阿里百炼对接本地大模型Ollama实现动态切换模型能力前端页面联调与展示二、环境准备与项目初始化后端spring官网https://spring.io/创建 Spring Boot 项目使用 Spring Initializr 或 IDEA 创建新项目注意 JDK 版本建议17Spring Boot 版本建议3.2Spring AI 要求。创建file1.SwaggerConfig类import io.swagger.v3.oas.models.ExternalDocumentation; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class SwaggerConfig { /* 配置swagger基本信息 */ Bean public OpenAPI swaggerOpenApi() { return new OpenAPI() .info(new Info().title(智能体项目) .description(智能体项目) .version(v1.0)) .externalDocs(new ExternalDocumentation() .description() .url()); } }2.HelloController类package edu.controller; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; RequestMapping(/test)//配置访问的前缀 RestController//配置交给spring容器管理配置返回json数据 Tag(name 测试接口) public class HelloController { GetMapping(/hello)//配置访问方式和路径 Operation(summary hello接口) public String hello(){ System.out.println(hello方法被访问了...); return hello world; } }3.SpringAgentApplication类package edu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; SpringBootApplication//启动类 public class SpringAgentApplication { public static void main(String[] args) { SpringApplication.run(SpringAgentApplication.class, args); } }4.application.yml下的fileserver: port: 8080 servlet: context-path: /5.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 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion groupIdcom.itheima/groupId artifactIdspring_agent/artifactId version1.0-SNAPSHOT/version !--下面都是新加的内容-- properties maven.compiler.source17/maven.compiler.source maven.compiler.target17/maven.compiler.target project.build.sourceEncodingUTF-8/project.build.sourceEncoding /properties !--把SpringBoot作为父工程-- parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.4.2/version /parent dependencies !--导入springspringmvc环境-- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency dependency groupIdorg.springdoc/groupId artifactIdspringdoc-openapi-starter-webmvc-ui/artifactId version2.6.0/version /dependency !-- 让响应结果更美观 -- dependency groupIdcom.alibaba.cola/groupId artifactIdcola-component-dto/artifactId version4.3.2/version /dependency dependency groupIdio.swagger.core.v3/groupId artifactIdswagger-models-jakarta/artifactId version2.2.22/version /dependency /dependencies !--下面都是新加的内容-结束-- /project所有文件夹情况最后点击运行访问这个网站http://localhost:8080/swagger-ui/index.html#/%E6%B5%8B%E8%AF%95%E6%8E%A5%E5%8F%A3/hellohttp://localhost:8081/swagger-ui/index.html三、实战对接多模型平台接入1.对接 OpenAI (云端)配置 API Key 后直接注入 ChatClient即可使用。新建ChatController类普通调用把内容一个一个敲给你流式调用把内容一股脑地发给你2.导入本地大模型SpringAI对接ollama3.SpringAI对接DeepSeekdeepseek API文档官网https://api-docs.deepseek.com/zh-cn/quick_start/pricing4.SpringAI对接阿里百炼硅基流动阿里云白炼API官网https://www.aliyun.com/product/bailian是这个https://bailian.console.aliyun.com/cn-beijing/#/home切换大模型四、项目演示前端访问学习看bug访问这个页面能访问前端http://localhost:8080/chat.html
Spring AI 实战全攻略|手把手教你对接 OpenAI、Ollama、DeepSeek、阿里百炼!从环境搭建到前端联调,一篇搞定!
一、项目背景与目标本系列旨在通过实战案例掌握 Spring AI 的核心用法。我们将构建一个支持多模型切换的聊天后端并提供前端页面进行交互测试。核心功能点基于 Spring Boot 3.x Spring AI 快速起步集成 Swagger/Knife4j 进行接口调试对接云端大模型OpenAI, DeepSeek, 阿里百炼对接本地大模型Ollama实现动态切换模型能力前端页面联调与展示二、环境准备与项目初始化后端spring官网https://spring.io/创建 Spring Boot 项目使用 Spring Initializr 或 IDEA 创建新项目注意 JDK 版本建议17Spring Boot 版本建议3.2Spring AI 要求。创建file1.SwaggerConfig类import io.swagger.v3.oas.models.ExternalDocumentation; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class SwaggerConfig { /* 配置swagger基本信息 */ Bean public OpenAPI swaggerOpenApi() { return new OpenAPI() .info(new Info().title(智能体项目) .description(智能体项目) .version(v1.0)) .externalDocs(new ExternalDocumentation() .description() .url()); } }2.HelloController类package edu.controller; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; RequestMapping(/test)//配置访问的前缀 RestController//配置交给spring容器管理配置返回json数据 Tag(name 测试接口) public class HelloController { GetMapping(/hello)//配置访问方式和路径 Operation(summary hello接口) public String hello(){ System.out.println(hello方法被访问了...); return hello world; } }3.SpringAgentApplication类package edu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; SpringBootApplication//启动类 public class SpringAgentApplication { public static void main(String[] args) { SpringApplication.run(SpringAgentApplication.class, args); } }4.application.yml下的fileserver: port: 8080 servlet: context-path: /5.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 http://maven.apache.org/xsd/maven-4.0.0.xsd modelVersion4.0.0/modelVersion groupIdcom.itheima/groupId artifactIdspring_agent/artifactId version1.0-SNAPSHOT/version !--下面都是新加的内容-- properties maven.compiler.source17/maven.compiler.source maven.compiler.target17/maven.compiler.target project.build.sourceEncodingUTF-8/project.build.sourceEncoding /properties !--把SpringBoot作为父工程-- parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.4.2/version /parent dependencies !--导入springspringmvc环境-- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-web/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId /dependency dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency dependency groupIdorg.springdoc/groupId artifactIdspringdoc-openapi-starter-webmvc-ui/artifactId version2.6.0/version /dependency !-- 让响应结果更美观 -- dependency groupIdcom.alibaba.cola/groupId artifactIdcola-component-dto/artifactId version4.3.2/version /dependency dependency groupIdio.swagger.core.v3/groupId artifactIdswagger-models-jakarta/artifactId version2.2.22/version /dependency /dependencies !--下面都是新加的内容-结束-- /project所有文件夹情况最后点击运行访问这个网站http://localhost:8080/swagger-ui/index.html#/%E6%B5%8B%E8%AF%95%E6%8E%A5%E5%8F%A3/hellohttp://localhost:8081/swagger-ui/index.html三、实战对接多模型平台接入1.对接 OpenAI (云端)配置 API Key 后直接注入 ChatClient即可使用。新建ChatController类普通调用把内容一个一个敲给你流式调用把内容一股脑地发给你2.导入本地大模型SpringAI对接ollama3.SpringAI对接DeepSeekdeepseek API文档官网https://api-docs.deepseek.com/zh-cn/quick_start/pricing4.SpringAI对接阿里百炼硅基流动阿里云白炼API官网https://www.aliyun.com/product/bailian是这个https://bailian.console.aliyun.com/cn-beijing/#/home切换大模型四、项目演示前端访问学习看bug访问这个页面能访问前端http://localhost:8080/chat.html