一、项目背景与目标
本系列旨在通过实战案例,掌握 Spring AI 的核心用法。我们将构建一个支持多模型切换的聊天后端,并提供前端页面进行交互测试。

核心功能点:
- 基于 Spring Boot 3.x + Spring AI 快速起步
- 集成 Swagger/Knife4j 进行接口调试
- 对接云端大模型(OpenAI, DeepSeek, 阿里百炼)
- 对接本地大模型(Ollama)
- 实现动态切换模型能力
- 前端页面联调与展示
二、环境准备与项目初始化
后端spring官网:https://spring.io/

创建 Spring Boot 项目
使用 Spring Initializr 或 IDEA 创建新项目,注意 JDK 版本建议 17+,Spring Boot 版本建议 3.2+(Spring AI 要求)。






创建file





1.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(下的file)
server:
port: 8080
servlet:
context-path: /
5.pom.xml(系统自带)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>spring_agent</artifactId>
<version>1.0-SNAPSHOT</version>
<!--下面都是新加的内容-->
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--把SpringBoot作为父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.2</version>
</parent>
<dependencies>
<!--导入spring+springmvc环境-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
</dependency>
<!-- 让响应结果更美观 -->
<dependency>
<groupId>com.alibaba.cola</groupId>
<artifactId>cola-component-dto</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-models-jakarta</artifactId>
<version>2.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/hello
http://localhost:8081/swagger-ui/index.html
三、实战对接:多模型平台接入
1.对接 OpenAI (云端)
配置 API Key 后,直接注入 ChatClient即可使用。



新建ChatController类


普通调用
(把内容一个一个敲给你)


流式调用
(把内容一股脑地发给你)

2.导入本地大模型SpringAI对接ollama



3.SpringAI对接DeepSeek




deepseek API文档官网:https://api-docs.deepseek.com/zh-cn/quick_start/pricing


4.SpringAI对接阿里百炼&硅基流动
阿里云白炼API官网:https://www.aliyun.com/product/bailian
是这个:https://bailian.console.aliyun.com/cn-beijing/#/home



切换大模型:



四、项目演示:前端访问

学习看bug


访问这个页面,能访问前端
http://localhost:8080/chat.html



2333

被折叠的 条评论
为什么被折叠?



