AI Automates a Spring Boot System, Leaving Colleagues Stunned
The article demonstrates how to turn ordinary Spring Boot methods into AI‑driven tools, enabling a language model to interpret a natural‑language request, orchestrate a multi‑step workflow (stock query, order creation, warehouse notification), and execute the entire business process without any hard‑coded if‑else logic.
This demo shows how a large language model can be used as an autonomous AI Agent to operate a Spring Boot application.
Core Idea
By annotating regular Spring Boot methods with @Tool, the methods become AI‑accessible tools. When a user sends a single natural‑language command such as “Help me create a MacBook Pro order and notify the warehouse”, the AI parses the intent, decides which tools to call, and executes them in the correct order.
Step‑by‑Step Setup
Project creation : Use JDK 17 and Spring Boot 3.3.x. Add the following Maven dependencies:
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>spring-ai-agent-demo</artifactId>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
</parent>
<properties>
<java.version>17</java.version>
<spring-ai.version>1.0.0</spring-ai.version>
</properties>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring AI -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>${spring-ai.version}</version>
</dependency>
</dependencies>
</project>AI model configuration : Add the model settings to application.yml.
server:
port: 8080
spring:
ai:
openai:
api-key: YOUR_API_KEY
base-url: https://api.openai.com
chat:
options:
model: gpt-4oAlternatively, use Claude via OpenRouter:
spring:
ai:
openai:
api-key: sk-xxxx
base-url: https://openrouter.ai/api/v1
chat:
options:
model: anthropic/claude-3.7-sonnetDefine Tools : Three Java classes are annotated with @Tool to expose them to the AI.
package com.demo.agent.tool;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Component;
@Component
public class StockTools {
@Tool(description = "查询商品库存")
public Integer queryStock(String productName) {
System.out.println("[Tool] 查询库存:" + productName);
return 99;
}
} package com.demo.agent.tool;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Component;
@Component
public class OrderTools {
@Tool(description = "创建订单")
public String createOrder(String productName) {
System.out.println("[Tool] 创建订单:" + productName);
return "订单创建成功";
}
} package com.demo.agent.tool;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Component;
@Component
public class WarehouseTools {
@Tool(description = "通知仓库发货")
public String sendWarehouseNotice(String productName) {
System.out.println("[Tool] 通知仓库发货:" + productName);
return "仓库已收到发货通知";
}
}AI Agent Controller : Wire the tools into a ChatClient and expose a /chat endpoint.
package com.demo.agent.controller;
import com.demo.agent.tool.OrderTools;
import com.demo.agent.tool.StockTools;
import com.demo.agent.tool.WarehouseTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AgentController {
private final ChatClient chatClient;
public AgentController(ChatClient.Builder builder, OrderTools orderTools, StockTools stockTools, WarehouseTools warehouseTools) {
this.chatClient = builder.defaultTools(orderTools, stockTools, warehouseTools).build();
}
@GetMapping("/chat")
public String chat(String message) {
return chatClient.prompt().user(message).call().content();
}
}Run the application with mvn spring-boot:run (or via an IDE). The console shows Tomcat started on port 8080.
Demo Execution
Send a request:
http://localhost:8080/chat?message=帮我购买一台MacBook%20Pro,并安排发货The AI automatically performs the workflow:
[Tool] 查询库存:MacBook Pro
[Tool] 创建订单:MacBook Pro
[Tool] 通知仓库发货:MacBook ProAnd returns:
MacBook Pro 库存充足。
订单已经创建成功。
仓库已经收到发货通知。Why Multiple Tools Are Invoked
The model first infers the user’s goal (purchase a product), then determines the necessary sub‑tasks: check stock, create an order, and notify the warehouse. It sequences the calls as queryStock() → createOrder() → sendWarehouseNotice(), which is a true AI‑driven workflow rather than a hard‑coded if‑else chain.
Future Extensions
While the demo uses three simple tools, the same pattern can be expanded to cover full enterprise micro‑service ecosystems (user service, payment service, ERP, CRM, etc.). The AI Agent could automatically orchestrate refund processing, approval flows, work‑order routing, and even generate SQL statements.
Takeaway
This example illustrates the shift from chat‑only AI to AI that can execute real business workflows, positioning Spring Boot as a foundational platform for the emerging AI Agent era.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
LuTiao Programming
LuTiao Programming is a friendly community offering free programming lessons. We inspire learners to explore new ideas and technologies and quickly acquire job-ready skills.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
