Getting Started with AI Chat in Spring Boot Using Spring AI Alibaba and Alibaba Cloud Baileian

This guide shows how to configure a Spring Boot 3 project with JDK 17, add the spring‑ai‑alibaba‑starter, obtain an Alibaba Cloud Baileian API key, and implement a simple AI chat controller for quick conversational testing.

The Dominant Programmer
The Dominant Programmer
The Dominant Programmer
Getting Started with AI Chat in Spring Boot Using Spring AI Alibaba and Alibaba Cloud Baileian

Many large‑model APIs offer free quotas that are ideal for individual developers or small projects. Alibaba Cloud’s Baileian (Tongyi) provides a free tier for new users, making it a convenient entry point for AI experiments.

The official Spring AI Alibaba starter enables seamless integration without needing OpenAI‑compatible adapters. By adding the starter dependency and configuring the API key, a ready‑to‑use ChatModel bean is created.

Setting Up JDK 17 for a Single Project

Keep the global JDK 8 unchanged and install JDK 17 locally. Extract the JDK to a directory without adding it to PATH or modifying system environment variables. In IntelliJ IDEA, open Project Structure (Ctrl+Alt+Shift+S), set the Project SDK to the JDK 17 directory, and set the language level to 17. Then, in Settings → Build → Compiler → Java Compiler , set the bytecode version to 17, ensuring only this project uses the newer JDK.

Obtaining an Alibaba Cloud Baileian API Key

Log in to the Baileian console at https://bailian.console.aliyun.com/, select the “North China 2 (Beijing)” region, navigate to the “API Key” page, create a new key, and copy it. Complete real‑name verification first. To avoid unexpected charges after the free quota is exhausted, enable the “stop when free quota is used up” option.

Creating the Spring Boot Project

Add the spring-ai-alibaba-starter dependency and the Spring Milestones repository to pom.xml:

org.springframework.boot:spring-boot-starter-parent:3.2.5
com.example:spring-ai-alibaba-bailian:1.0
    17
    1.0.0-M5.1
        org.springframework.boot:spring-boot-starter-web
        com.alibaba.cloud.ai:spring-ai-alibaba-starter:1.0.0-M5.1
        spring-milestones
        https://repo.spring.io/milestone

In application.yml, configure the API key and model:

spring:
  ai:
    dashscope:
      api-key: YOUR_API_KEY
    chat:
      options:
        model: qwen-max

Create a simple chat controller:

package com.badao.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatController {
    private final ChatClient chatClient;
    public ChatController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }
    @GetMapping("/ai/generate")
    public String generate(@RequestParam(value = "message", defaultValue = "你好") String message) {
        return chatClient.prompt()
                .user(message)
                .call()
                .content();
    }
}

Run the application and test the /ai/generate endpoint. The following screenshot shows a successful response:

AI chat test result
AI chat test result
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

spring-bootSpring AItutorialAlibaba CloudAI chatJDK 17Baileian
The Dominant Programmer
Written by

The Dominant Programmer

Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi

0 followers
Reader feedback

How this landed with the community

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.