AI is no longer just a front-end feature; it is rapidly becoming a first-class person of back-end engineering. Whether you are creating intelligent APIs, automations, or context-based microservices, the ability to plug Large Language Models (LLMs) into Spring Boot can become a superpower.
But thanks to Spring AI, it's not as difficult as it seems.
In this tutorial, we'll show you how to integrate Google Gemini (one of the most developed multimodal models on the market today) into your Spring Boot application
If you are a Java engineer, this tutorial will provide everything you need to get started in no time.
Why Spring AI + Gemini Makes Sense
Spring AI eliminates the need for any significant AI plumbing, leaving you with only:
Instead, you get nice and clean Java abstractions that really are:
\
ChatResponseModel res = chatClient.prompt("Explain Spring AI and its "). call(); //chaClient is the clinet provided by SpringAI to intrearct with
\ Gemini offers:
GeMini+ Spring AI turns your simple Java into an AI-powered backend engine.
Now we look into the required coding changes for a simple Java AI project.
\ \
<dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-gemini-spring-boot-starter</artifactId> </dependency> # Spring config need spring.ai.gemini.api-key= its YOUR_API_KEY spring.ai.gemini.model=gemini-pro { what ever AI model you want to integrate you can add here } #gemini-pro-vision if you need use vision
\ Now it's is the time to build a simple chat API
\
@RestController @RequestMapping("/ai") public class GeminiRestController { private final ChatClient chatClient; public GeminiRestController(ChatClient chatClient) { this.chatClient = chatClient; } @GetMapping("/explain") public String explain(@RequestParam String topic) { ChatResponse response = chatClient.prompt("Explain " + topic + " in simple terms.").call(); return response.getResult().getOutputText(); } }
\ Do you need Multimodal (image)? Query Support
\
@PostMapping(value = "/vision", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public String vision(@RequestPart MultipartFile file) throws IOException { Prompt prompt = Prompt.builder() .text("Describe requiured image in detail.") .media(file.getBytes(), file.getContentType()) .build(); ChatResponse response = chatClient.prompt(prompt).call(); return response.getResult().getOutputText(); }
\ \
\
\ Sanitizing your prompt:
\
public String sanitize(String input) { return input .replaceAll("(?i)delete|drop|shutdown|ignore previous|system:", "") .trim(); } // now use this
String cleaned = sanitizeInput(userText); chatClient.prompt(cleaned).call();
``` \ \ Only users who are authenticated should be allowed to call`/ai` the endpoint. We will use Role-Based Access Control (RBAC) for this \ \
java @PreAuthorize("hasRole('AI_USER')") @GetMapping("/explain") public String explain(@RequestParam String topic) {
}
@EnableWebSecurity public class SecurityConfig {
@Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeHttpRequests(auth -> auth .requestMatchers("/ai/**").authenticated() .anyRequest().permitAll() ) .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt); return http.build(); }
}
\ Protect **sensi**tive data logging. Masking: You never want to log raw prompts or responses. \ \
java Slf4j @Service public class SecureAIDataService {
public String safeLog(String text) { return text.replaceAll("[0-9]{12,}", "****MASKED****"); } public String callAi(String input) { log.info("AI Prompt: {}", safeLog(input)); ChatResponse response = chatClient.prompt(input).call(); String output = safeLog(response.getResult().getOutputText()); log.info("AI Output: {}", output); return output; }
} ```
\


