Boost Spring AI with Tool Calls: Real‑World Spring Boot 3 Examples
This article explains how Spring AI's tool‑calling feature extends large language models by integrating custom Java tools for information retrieval and operations, and provides step‑by‑step Spring Boot 3 examples—including date‑time and weather tools—complete with configuration, code snippets, and execution results.
Spring AI enables tool (function) calling to extend large language model capabilities, allowing models to retrieve information or perform actions via external APIs.
Tools are categorized into information retrieval and operation execution, and are defined with @Tool annotation providing name, description, and other metadata.
Example 1 demonstrates creating a tool that returns the current date and time, configuring it in ChatClient , and invoking it through a REST endpoint.
<code><dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>1.0.0-M6.1</version>
</dependency></code> <code>spring:
ai:
dashscope:
api-key: sk-********
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
chat:
options:
model: qwen-turbo</code> <code>@RestController
@RequestMapping("/tools")
public class ToolController {
private final ChatClient chatClient;
public ToolController(ChatClient.Builder aiClientBuilder) {
this.chatClient = aiClientBuilder.build();
}
@GetMapping("/getDate")
public ResponseEntity<?> getDate(String prompt) {
String response = this.chatClient.prompt(prompt).call().content();
return ResponseEntity.ok(response);
}
}</code>Example 2 defines a WeatherTools class that calls an external weather API, registers it with ChatClient , and obtains weather data through a REST endpoint.
<code>@Tool(description = "获取当前天气预报")
public class WeatherTools {
String getCurrentWeather(String city) {
RestClient client = RestClient.create(URI.create("https://api.vvhan.com"));
Map<?,?> result = client.get()
.uri("/api/weather?city={0}", city)
.retrieve()
.body(Map.class);
try {
return new ObjectMapper().writeValueAsString(result);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}</code> <code>@GetMapping("/weather")
public ResponseEntity<String> getCurrentWeather(String prompt) {
String response = this.chatClient.prompt(prompt)
.tools(new WeatherTools())
.call().content();
return ResponseEntity.ok(response);
}</code>Tools can also be defined as Spring beans using @Bean and @Description , allowing functions, suppliers, consumers, or bifunctions to serve as tools.
<code>@Bean(CURRENT_WEATHER)
@Description("获取当前天气预报")
Function<WeatherRequest, WeatherResponse> currentWeather() {
RestClient client = RestClient.create(URI.create("https://api.vvhan.com"));
return city -> {
String result = client.get()
.uri("/api/weather?city={0}", city)
.retrieve()
.body(String.class);
return new WeatherResponse(result);
};
}</code>After registering the tools, calling the endpoints returns enriched responses, demonstrating successful integration of tool calling in Spring Boot 3 applications.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.