Integrating LangChain4j with Spring Boot: AI Services Made Easy

This guide explains how to use LangChain4j Spring Boot starters to configure OpenAI models, create declarative AI services with @AiService, and run practical examples such as a customer‑support agent and a simple HelloWorld program, covering required dependencies, properties, and code snippets.

JavaEdge
JavaEdge
JavaEdge
Integrating LangChain4j with Spring Boot: AI Services Made Easy

LangChain4j provides Spring Boot starters that simplify the integration of large language models (LLMs) and related AI components into Java applications.

0. Introduction

The starters support common integrations and declarative AI services, allowing developers to configure models and inject them as beans.

1. Common Integration Starters

Each starter follows the naming pattern langchain4j-{integration-name}-spring-boot-starter. For example, the OpenAI starter is langchain4j-open-ai-spring-boot-starter and can be added with the following Maven dependency:

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
    <version>0.34.0</version>
</dependency>

Model parameters are configured in application.properties:

langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY}
langchain4j.open-ai.chat-model.model-name=gpt-4o
langchain4j.open-ai.chat-model.log-requests=true
langchain4j.open-ai.chat-model.log-responses=true
...

Spring Boot will automatically create an OpenAiChatModel bean that implements ChatLanguageModel.

The bean can be injected and used directly:

@RestController
public class ChatController {
    private final ChatLanguageModel chatLanguageModel;
    public ChatController(ChatLanguageModel chatLanguageModel) {
        this.chatLanguageModel = chatLanguageModel;
    }
    @GetMapping("/chat")
    public String model(@RequestParam(value = "message", defaultValue = "Hello") String message) {
        return chatLanguageModel.generate(message);
    }
}

For streaming responses, use the streaming-chat-model property to obtain a StreamingChatLanguageModel bean.

2. Declarative AI Service Starter

The langchain4j-spring-boot-starter enables automatic configuration of AI services, Retrieval‑Augmented Generation (RAG), and tools.

Define an interface annotated with @AiService:

@AiService
interface Assistant {
    @SystemMessage("You are a polite assistant")
    String chat(String userMessage);
}

At startup, the starter scans for @AiService interfaces, creates implementations using all available LangChain4j components, and registers them as Spring beans. They can then be injected like regular services:

@RestController
class AssistantController {
    @Autowired
    Assistant assistant;
    @GetMapping("/chat")
    public String chat(String message) {
        return assistant.chat(message);
    }
}

3. Supported Versions

LangChain4j Spring Boot integration requires Java 17 and Spring Boot 3.2.

4. Examples

Low‑level Spring Boot example using ChatLanguageModel API.

High‑level Spring Boot example using declarative AI services.

4.1 Customer Support Agent Example

After cloning the official repository, set the OpenAI API key in application.properties and run CustomerSupportAgentApplication. Interact via the console:

Query: How can I cancel my booking?

The AI asks for additional information because a tool is registered:

@Component
public class BookingTools {
    @Autowired
    private BookingService bookingService;
    @Tool
    public void cancelBooking(String bookingNumber, String customerName, String customerSurname) {
        System.out.printf("[Tool]: Cancelling booking %s for %s %s...%n", bookingNumber, customerName, customerSurname);
        bookingService.cancelBooking(bookingNumber, customerName, customerSurname);
    }
}

Providing the requested data leads to a backend‑thrown business exception (booking not found), which the AI reports and then prompts for correct input.

4.2 HelloWorld Example

import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import static dev.langchain4j.model.openai.OpenAiChatModelName.GPT_4_O_MINI;

public class _00_HelloWorld {
    public static void main(String[] args) {
        ChatLanguageModel model = OpenAiChatModel.builder()
                .apiKey(ApiKeys.OPENAI_API_KEY)
                .modelName(GPT_4_O_MINI)
                .build();
        String answer = model.generate("Say Hello World");
        System.out.println(answer);
    }
}

Output: Hello, World! The article provides all necessary Maven coordinates, configuration properties, and code snippets to get started quickly with LangChain4j in a Spring Boot environment.

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.

JavaSpring BootLangchain4jChatLanguageModelDeclarative AI Service
JavaEdge
Written by

JavaEdge

First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.

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.