跪拜 Guibai
← All articles
AI Programming · OpenAI

Spring AI's ChatClient Turns a Java CRUD App Into a Poetry Bot in Under 50 Lines

By 第一行代码HW ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Spring AI 1.0 gives the enormous Java/Spring ecosystem a stable, RestTemplate-like on-ramp to LLMs. Teams that already run Spring Boot in production can add AI calls without leaving their stack, their dependency-injection patterns, or their configuration-management habits.

Summary

A new Spring AI 1.0 starter—spring-ai-starter-model-openai—replaces the hand-rolled HTTP, JSON parsing, and error handling that Java developers previously needed to call an LLM. The ChatClient API mirrors the ergonomics of RestTemplate: a builder-injected bean, a prompt().user().call().content() chain, and model-provider details pushed entirely into application.yml. The walkthrough targets Tongyi Qianwen via Alibaba Cloud's OpenAI-compatible endpoint, using an environment-variable API key to avoid credential leaks.

The tutorial also draws a clean line between Spring AI (the portable interface layer) and Spring AI Alibaba (an Alibaba Cloud-specific superset with Graph workflow support), framing the choice as JDBC vs. a vendor driver. A refactor step separates concerns into a Controller and Service, producing a REST endpoint that turns a query parameter into an AI-generated poem.

Common early pitfalls are catalogued: the base-url path must end at /compatible-mode without a trailing /v1, hardcoded keys in YAML invite credential theft, and model names like qwen-plus vs. qwen-max carry real performance differences.

Takeaways
Spring AI 1.0 GA renamed the OpenAI starter to spring-ai-starter-model-openai; older artifact names like spring-ai-starter-openai are deprecated and will break new projects.
ChatClient is the LLM equivalent of RestTemplate—one builder-injected bean, one chained call, and model-provider details live in application.yml.
Spring AI is the portable interface layer; Spring AI Alibaba is a vendor-specific superset that adds Alibaba Cloud Bailian integration and a Graph workflow engine.
Tongyi Qianwen's OpenAI-compatible endpoint is /compatible-mode, not /compatible-mode/v1; the extra suffix returns a 404.
API keys should be injected via ${AI_API_KEY} from environment variables, never hardcoded in YAML files that might reach a Git remote.
Model names like qwen-plus, qwen-turbo, and qwen-max are distinct; picking the wrong one changes cost and output quality.
Separating ChatClient usage into a Service and Controller takes only a few extra lines and makes the AI call available as a REST endpoint.
Conclusions

Spring AI's design choice to mirror RestTemplate lowers the cognitive overhead for millions of Spring developers, but it also inherits RestTemplate's limitations—blocking calls by default, no built-in retry or circuit-breaking, and a builder that hides the underlying HTTP client configuration.

The JDBC-vs-driver analogy for Spring AI vs. Spring AI Alibaba is useful but incomplete: JDBC drivers don't add entirely new execution engines like a Graph workflow, so the Alibaba variant is more of a platform SDK than a drop-in driver.

Hardcoding prompts inside Java source files is called out as a problem to solve in the next installment, which signals that the Spring AI ecosystem still lacks a mature, community-standard prompt templating story comparable to what Python frameworks offer.

Concepts & terms
ChatClient
Spring AI's primary API for conversational LLM calls. Injected via a builder, it exposes a fluent prompt().user().call().content() chain that abstracts HTTP requests, authentication, and response parsing across model providers.
Spring AI Alibaba
An Alibaba Cloud-specific extension of Spring AI that adds deep integration with the Bailian platform and a Graph workflow engine, analogous to a vendor-specific JDBC driver with extra proprietary features.
OpenAI-compatible interface
An HTTP API that mimics OpenAI's /v1/chat/completions endpoint schema, allowing any client built for OpenAI to work with alternative providers like Tongyi Qianwen by changing only the base URL and API key.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗