Building an AI-Powered Chatbot with Spring Boot and DeepSeek
This tutorial demonstrates how to create an AI-driven Spring Boot application by integrating DeepSeek's large language model, covering project setup, dependency configuration, API key management, and implementing a REST controller that provides weather forecasts via a conversational interface.
In the digital era, artificial intelligence (AI) is a core driver of innovation, and Spring Boot remains a popular Java framework for building enterprise back‑end services. Combining the two enables developers to quickly create AI‑enhanced applications.
The article explains how integrating DeepSeek’s large language model with Spring Boot allows the creation of intelligent features such as chatbots, document processing, and code generation, providing users with personalized, smart experiences.
To start, a new Spring Boot project is generated with the web and ai starters selected. After project creation, the necessary dependencies are automatically added.
The required Maven configuration is shown below:
<properties>
<java.version>17</java.version>
<spring-ai.version>1.0.0-M5</spring-ai.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Next, the OpenAI (DeepSeek) related parameters are configured. After registering on the SiliconFlow platform, an API key is obtained and the model name deepseek-ai/DeepSeek-R1-Distill-Llama-8B is selected.
spring:
ai:
openai:
api-key: YOUR_API_KEY
base-url: https://api.siliconflow.cn
chat:
options:
model: deepseek-ai/DeepSeek-R1-Distill-Llama-8BA REST controller is then created to expose a chat endpoint. The controller sets a system role that acts as a weather forecaster for Suzhou; when a date is supplied, the model returns the weather forecast in markdown format.
package com.summer.springai.controller;
import groovy.util.logging.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin(origins = "*")
@Slf4j
public class ChatBotController {
private final ChatClient chatClient;
public ChatBotController(ChatClient.Builder builder) {
this.chatClient = builder.defaultSystem(
"你是一个天气预报员,当有人输入日期的时候,你输出苏州的天气预报信息," +
"生成结果在html页面中以markdown的格式输出,最后输出结尾的时候始终以下面的语句结尾:感谢您的咨询,我是舆情君。"
).build();
}
@GetMapping(value = "/chat/{message}")
public String chat(@PathVariable("message") String message) {
return chatClient.prompt()
.user(message)
.call()
.content();
}
}After building and running the application, the AI service can be invoked via a URL such as http://localhost:8080/ai/chat/2025年2月12日 , which returns the weather forecast generated by DeepSeek.
The article concludes by noting that this simple demo can be extended to output JSON, store results in a database, or perform data collection for enterprise projects.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.