Spring AI Hands‑On for Java Developers: Connecting ChatClient to the MiniMax LLM
This tutorial shows Java engineers how to set up a Spring Boot 4 project, configure Spring AI for the MiniMax large‑language model, call it via simple and streaming endpoints, use prompt templates with dynamic parameters, add metadata and advisors, and switch between different LLM providers with minimal code changes.
Project setup
pom.xml includes Spring Boot 4.0.5, Spring AI 2.0.0‑M4, WebFlux starter, and the MiniMax auto‑configuration starter.
<!-- Spring Boot 4.0.5 + Spring AI 2.0.0-M4 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.5</version>
</parent>
<dependencies>
<!-- WebFlux for streaming responses -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- MiniMax auto‑configuration -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-minimax</artifactId>
</dependency>
</dependencies>Configuration
Three properties in application.properties are sufficient:
spring.ai.minimax.base-url=api.minimax.chat
spring.ai.minimax.api-key=YOUR_API_KEY
spring.ai.minimax.chat.options.model=minimax-m2.7No custom configuration class is required; Spring Boot auto‑configuration creates the necessary beans.
Simple chat endpoint
A @RestController exposing GET /minimax/call?question=... builds a ChatClient from the injected MiniMaxChatModel, sends the prompt, and returns the response as a Mono<String>:
@RestController
@RequestMapping("minimax")
public class MiniMaxController {
@Resource
private MiniMaxChatModel miniMaxChatModel; // auto‑injected
@GetMapping("/call")
public Mono<String> call(@RequestParam("question") String question) {
ChatClient chatClient = ChatClient.builder(miniMaxChatModel).build();
return Mono.fromCallable(() ->
chatClient.prompt(new Prompt(question))
.call()
.content())
.subscribeOn(Schedulers.boundedElastic());
}
}Streaming (SSE) response
Endpoint GET /stream/str produces MediaType.TEXT_EVENT_STREAM_VALUE and returns a Flux<String> where each token is emitted separately, enabling a “typewriter” effect:
@GetMapping(value = "/stream/str", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamStr(@RequestParam("question") String question) {
ChatClient chatClient = ChatClient.create(miniMaxChatModel);
return chatClient.prompt(question)
.stream()
.content(); // each character emitted
}Prompt templates with dynamic parameters
Using the template syntax, variables are written as {variableName} and replaced at runtime via .param(). Example endpoint /call/prompt injects a player parameter into the prompt:
@GetMapping("/call/prompt")
public Mono<String> callPrompt(@RequestParam("player") String player) {
ChatClient chatClient = ChatClient.create(miniMaxChatModel);
return Mono.fromCallable(() ->
chatClient.prompt()
.user(u -> u.text("请输出NBA球员{player}的最高得分")
.param("player", player))
.call()
.content())
.subscribeOn(Schedulers.boundedElastic());
}Metadata and advisors
Metadata can be attached to a request, and advisors act as interceptors. The following endpoint adds a metadata pair and a logging advisor:
@GetMapping(value = "/stream/metadata", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamMetadata(@RequestParam("question") String question) {
ChatClient chatClient = ChatClient.create(miniMaxChatModel);
return chatClient.prompt()
.user(u -> u.text(question).metadata("my name", "why"))
.advisors(new SimpleLoggerAdvisor())
.stream()
.content();
}Built‑in advisors listed by Spring AI: SimpleLoggerAdvisor – logs request and response. MessageChatMemoryAdvisor – provides multi‑turn conversation memory. QuestionAnswerAdvisor – adds retrieval‑augmented generation.
Multi‑model support
Switching to a different provider only requires changing the injected model bean; the same ChatClient code works unchanged:
// Same ChatClient, different model bean
ChatClient chatClient = ChatClient.create(miniMaxChatModel); // MiniMax
ChatClient chatClient = ChatClient.create(openAiChatModel); // OpenAI
ChatClient chatClient = ChatClient.create(deepseekChatModel); // DeepSeekSource code
Full source code is available at https://github.com/你的github
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Coder Circle
Limited experience, continuously learning and summarizing knowledge, aiming to join a top tech company.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
