Backend Development 9 min read

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 Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Boost Spring AI with Tool Calls: Real‑World Spring Boot 3 Examples

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>&lt;dependency&gt;
  &lt;groupId&gt;com.alibaba.cloud.ai&lt;/groupId&gt;
  &lt;artifactId&gt;spring-ai-alibaba-starter&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-M6.1&lt;/version&gt;
&lt;/dependency&gt;</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.

The main sequence of actions for tool calling
The main sequence of actions for tool calling
Javabackend developmentSpring BootSpring AIAI integrationTool Calling
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.