Skip to the content.

Template

Information

Spring Boot is a strong choice for building AI-enabled backend services, internal tools, chat applications, document-processing pipelines, and workflow automations. It already provides the pieces typically needed around AI: REST APIs, configuration management, security, observability, validation, scheduling, messaging, data access, batch jobs, and integration with external systems.

For AI use cases, Spring Boot is often used as the orchestration layer around model providers, vector databases, business rules, and enterprise data sources.

AI integrations in Spring Boot and Java

Common possibilities:

Main tools and frameworks

Spring AI

Spring AI is the most Spring-native option for AI integrations in Spring Boot. It follows familiar Spring patterns such as autoconfiguration, starter dependencies, property-based configuration, abstractions for providers, and integration with vector stores and tool calling.

Good fit when you want:

LangChain4j

LangChain4j is the main Java ecosystem equivalent to LangChain concepts. It is useful for Java-first AI applications, especially when you want memory, retrieval, tool calling, AI services, and broader LLM application patterns without being tied only to Spring.

Good fit when you want:

Direct provider SDKs and HTTP clients

You can also integrate directly with provider SDKs or plain HTTP clients such as WebClient or RestClient.

Good fit when you want:

This is often useful for OpenAI-compatible APIs, Anthropic, Gemini, Azure OpenAI, local Ollama endpoints, or custom inference gateways.

Usage, tips and tricks

Generating

https://start.spring.io/#!type=maven-project&language=java&platformVersion=3.1.4&packaging=jar&jvmVersion=21&groupId=ee.test.task&artifactId=event-application&name=event-application&description=Event%20Registration%20Application&packageName=ee.test.task.event-application&dependencies=lombok,devtools,web,thymeleaf,jdbc,data-jdbc,data-jpa,liquibase,h2,postgresql,validation

Command line Runner

To inject dependencies.

package info.setmy.spring.boot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CLIApplication implements CommandLineRunner, ExitCodeGenerator {

    @Autowired
    SomeService someService;

    private int exitCode;

    public static void main(String[] args) {
        System.exit(
            SpringApplication.exit(
                SpringApplication.run(MergerApplication.class, args)
            )
        );
    }

    @Override
    public void run(String... args) throws Exception {
        readerService.processFiles();
        // Also changes in exitCode
    }

    @Override
    public int getExitCode() {
        return exitCode;
    }
}

Debugging

mvn spring-boot:run -Dagentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000

Or in pom.xml


<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring.boot.verions}</version>
    <configuration>
        <jvmArguments>jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
        </jvmArgumentsjvmArguments>
    </configuration>
</plugin>

Getting profiles in code


@Autowired
Environment environment;

Make the console silent

spring:
    main:
        banner-mode: "off"

Spring execution 1

# SB 3.x : org.springframework.boot.loader.launch.PropertiesLauncher
java ${JAVA_OPTIONS} -Dlogging.file.name=${LOG_DIR_NAME}/${LOG_FILE_NAME} -Dspring.profiles.active=${APPLICATION_PROFILES} -Dspring.config.additional-location=optional:${OPTIONAL_CONFIG_FILE_NAME} -Dloader.main=${APPLICATION_MAIN_CLASS_NAME} -cp ${APPLICATION_JAR_FILE_NAME} org.springframework.boot.loader.PropertiesLauncher ${*}

AI integration tips and tricks

Typical architecture

For production systems, keep the AI integration behind your own service layer:

This keeps prompt logic, provider-specific code, and business logic separate.

Configuration tips

Example:

app:
    ai:
        model: gpt-4o-mini
        temperature: 0.2
        timeout-seconds: 30

Design tips

RAG tips

Performance and operations

Security and compliance

Testing tips

Spring Boot AI stack examples

Option 1: Spring Boot + Spring AI

Best for Spring-native applications that want standard Boot configuration, AI client abstractions, and easier provider swapping.

Option 2: Spring Boot + LangChain4j

Best for Java-first LLM application patterns, AI services, memory, retrieval, and tool calling.

Option 3: Spring Boot + provider SDK

Best when you need low-level control or vendor-specific features immediately.

Option 4: Spring Boot + local models

Useful for local development, privacy-sensitive environments, and low-cost prototyping. A common path is running local models via Ollama and exposing them through the same application services used for cloud providers.

See also