Spring AI 实战全攻略|手把手教你对接 OpenAI、Ollama、DeepSeek、阿里百炼!从环境搭建到前端联调,一篇搞定!

AI 时代程序员必备技能

Codex、Claude Code、Cursor、Hermes Agent、OpenClaw等工程化实战专栏 ,讲透 AI 如何接管脏活累活

一、项目背景与目标

本系列旨在通过实战案例,掌握 Spring AI 的核心用法。我们将构建一个支持多模型切换的聊天后端,并提供前端页面进行交互测试。
在这里插入图片描述

核心功能点:

  1. 基于 Spring Boot 3.x + Spring AI 快速起步
  2. 集成 Swagger/Knife4j 进行接口调试
  3. 对接云端大模型(OpenAI, DeepSeek, 阿里百炼)
  4. 对接本地大模型(Ollama)
  5. 实现动态切换模型能力
  6. 前端页面联调与展示

二、环境准备与项目初始化

后端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

AI 时代程序员必备技能

Codex、Claude Code、Cursor、Hermes Agent、OpenClaw等工程化实战专栏 ,讲透 AI 如何接管脏活累活

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值