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

This guide shows how to set up a Spring Boot 3.x project with JDK 17, add the spring‑ai‑alibaba‑starter, configure an Alibaba Cloud Bailei API key, and implement a simple chat controller to call the model via Spring AI.

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

Many free large‑model APIs are available worldwide, making them ideal for individual developers or small projects. Alibaba Cloud's Bailei (Tongyi) service offers a free quota for new users, providing a convenient entry point for beginners.

Why use the official Spring AI Alibaba starter

The Spring AI team supplies dedicated starters for major providers (e.g., Google Gemini). The spring-ai-alibaba-starter integrates Bailei without requiring OpenAI‑compatible wrappers: you only need to place the API key in the configuration file to obtain an out‑of‑the‑box ChatModel bean.

Configure JDK 17 for a single project

Keep the global JDK 8 unchanged and install JDK 17 locally. In IntelliJ IDEA:

Open Project Structure (Ctrl+Alt+Shift+S).

Set Project SDK to the extracted JDK 17 directory and choose language level 17.

Navigate to Settings → Build → Compiler → Java Compiler and set the bytecode version to 17.

These steps affect only the current project, leaving other projects on JDK 8 untouched.

Obtain an Alibaba Cloud Bailei API key

Log in to the Bailei console at https://bailian.console.aliyun.com/, select the “China North‑Beijing” region, go to the “API Key” page, create a new key, and copy it. Ensure you have completed real‑name verification first. After the free quota is exhausted, the service switches to paid mode; you can enable the “stop when free quota is used up” option to avoid unexpected charges.

Create a Spring Boot project

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

<dependency>
    <groupId>com.alibaba.cloud.ai</groupId>
    <artifactId>spring-ai-alibaba-starter</artifactId>
    <version>1.0.0-M5.1</version>
</dependency>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <url>https://repo.spring.io/milestone</url>
    </repository>
</repositories>

Configure the model in application.yml (replace the placeholder with your own API key):

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

Implement a 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 and test

Start the Spring Boot application and call the /ai/generate?message=... endpoint. The response contains the AI‑generated reply from Bailei.

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.

AlibabaJavaspring-bootSpring AIjdk17AI chatbailei
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.