ChatClient is a Spring AI component for the common case of sending conversational text to a language model and receiving generated text. A Spring Boot service injects ChatClient.Builder, builds a client, and uses its fluent interface to construct and submit prompts. section 1.1.2 section 1.2.1
A basic request follows this sequence:
- Build the client from the injected ChatClient.Builder. section 1.1.2
- Start prompt construction with prompt(). section 1.1.2
- Place the question in the user role with user(). section 1.1.2
- Submit the completed prompt with call() and retrieve response text with content(). section 1.1.2
A service can wrap the returned text in an application-specific Answer record and expose the operation through a REST controller. ChatClient also supports tools supplied when the client is built or when an individual prompt is created. section 1.1.2 section 6.2.3
Board-game question service
A SpringAiBoardGameService receives a ChatClient.Builder, builds its client, sends question.question() through user(), calls the model, and returns new Answer(answerText). A controller accepts Question JSON at /ask and delegates to the service.
Listing 1.2 The Spring AI implementation of BoardGameService
package com.example.boardgamebuddy;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.stereotype.Service;
@Service
public class SpringAiBoardGameService implements BoardGameService {
private final ChatClient chatClient;
public SpringAiBoardGameService(ChatClient.Builder chatClientBuilder) { #1
this.chatClient = chatClientBuilder.build(); #2
}
@Override
public Answer askQuestion(Question question) {
var answerText = chatClient.prompt()
.user(question.question()) #3
.call()
.content();
return new Answer(answerText);
}
}
Inspect the complete service implementation showing constructor injection, client creation, fluent prompt construction, and response wrapping. section 1.1.2
The same abstraction supports summarization, translation, sentiment analysis, and agentic workflows; the prompt determines the task. Tools and workflow patterns extend its use beyond question answering. section 11.4 section 12.2
