Image Generation with Spring AI
Image generation is one of the most exciting features in modern AI applications. Instead of only generating text responses, AI systems can now create images from natural language prompts. With Spring AI, Java developers can integrate image generation capabilities into Spring Boot applications using familiar Spring patterns.
Spring AI provides an ImageModel API designed as a simple and portable interface for interacting with AI models specialized in image generation. This allows developers to switch between image model providers with less code change compared to writing provider-specific integrations manually. Spring AI Image Model API reference
What is AI Image Generation?
AI image generation means creating images from a text prompt.
Example prompt:
Create a modern illustration of a Java developer building an AI chatbot with Spring Boot.
The image model reads the prompt and generates a new image based on the description.
Simple Image Generation Flow
User Prompt
|
v
Spring Boot API
|
v
Spring AI ImageModel
|
v
Image Generation Provider
|
v
Generated Image URL / Image Data
Why Use Spring AI for Image Generation?
Without Spring AI, developers usually need to manually call provider APIs, build JSON requests, manage authentication, parse responses, and handle provider-specific behavior.
Spring AI provides a cleaner abstraction using:
ImageModelImagePromptImageResponse- Provider-specific configuration
- Spring Boot dependency injection
ImageModel, ImagePrompt, and ImageResponse
| Component | Purpose |
|---|---|
ImageModel |
Interface used to call image generation models |
ImagePrompt |
Represents the prompt and image generation options |
ImageResponse |
Contains generated image result information |
The Spring AI image API is built around sending an ImagePrompt to an ImageModel and receiving an ImageResponse. Spring AI Image Model API reference
Real-Time Use Cases
1. Learning Platform
A learning platform can generate course thumbnails, topic illustrations, concept diagrams, and promotional banners.
Prompt:
Create a clean course thumbnail for "Spring AI with Java",
showing Java, Spring Boot, AI, vector database, and chatbot icons.
2. E-Commerce
An e-commerce application can generate marketing banners, category visuals, product concept images, and campaign creatives.
Prompt:
Create a festive sale banner for electronics with laptops,
headphones, mobile phones, and bright modern lighting.
3. Banking
A banking app can generate educational graphics for UPI safety, card protection, loan awareness, and fraud prevention.
Prompt:
Create a simple educational illustration showing safe UPI payment practices.
4. Blog and SEO Content
Technical websites can generate unique visual assets for articles such as Docker, Kubernetes, Spring AI, RAG, and Agentic AI.
Step 1: Create Spring Boot Project
Create a Spring Boot project with:
- Java 17 or later
- Spring Web
- Spring Boot Actuator
- Spring AI OpenAI starter
Step 2: Add Spring AI BOM
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Step 3: Add OpenAI Starter Dependency
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
Spring AI supports OpenAI image generation models through its OpenAI image integration. The OpenAI image documentation explains that Spring AI supports DALL-E image generation and uses the spring.ai.openai.api-key property for the OpenAI API key. Spring AI OpenAI Image reference
Step 4: Configure application.properties
spring.application.name=spring-ai-image-demo
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.image.options.model=dall-e-3
spring.ai.openai.image.options.size=1024x1024
spring.ai.openai.image.options.quality=standard
spring.ai.openai.image.options.style=vivid
management.endpoints.web.exposure.include=health,info,metrics
Do not hardcode real API keys. Use environment variables, Kubernetes Secrets, or a cloud secret manager.
Environment Variable Example
Linux or macOS
export OPENAI_API_KEY=your_openai_api_key_here
Windows PowerShell
$env:OPENAI_API_KEY="your_openai_api_key_here"
Step 5: Create Image Request DTO
package com.dhanish.image.dto;
public class ImageGenerationRequest {
private String prompt;
public ImageGenerationRequest() {
}
public ImageGenerationRequest(String prompt) {
this.prompt = prompt;
}
public String getPrompt() {
return prompt;
}
public void setPrompt(String prompt) {
this.prompt = prompt;
}
}
Step 6: Create Image Response DTO
package com.dhanish.image.dto;
public class ImageGenerationResponse {
private String imageUrl;
public ImageGenerationResponse(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getImageUrl() {
return imageUrl;
}
}
Step 7: Create Image Generation Service
package com.dhanish.image.service;
import org.springframework.ai.image.ImageModel;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.stereotype.Service;
@Service
public class ImageGenerationService {
private final ImageModel imageModel;
public ImageGenerationService(ImageModel imageModel) {
this.imageModel = imageModel;
}
public String generateImage(String prompt) {
if (prompt == null || prompt.isBlank()) {
throw new IllegalArgumentException("Prompt is required.");
}
if (prompt.length() > 1000) {
throw new IllegalArgumentException("Prompt is too long.");
}
ImageResponse response = imageModel.call(
new ImagePrompt(prompt)
);
return response.getResult()
.getOutput()
.getUrl();
}
}
Step 8: Create REST Controller
package com.dhanish.image.controller;
import com.dhanish.image.dto.ImageGenerationRequest;
import com.dhanish.image.dto.ImageGenerationResponse;
import com.dhanish.image.service.ImageGenerationService;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/images")
public class ImageGenerationController {
private final ImageGenerationService imageService;
public ImageGenerationController(ImageGenerationService imageService) {
this.imageService = imageService;
}
@PostMapping("/generate")
public ImageGenerationResponse generate(
@RequestBody ImageGenerationRequest request) {
String imageUrl = imageService.generateImage(request.getPrompt());
return new ImageGenerationResponse(imageUrl);
}
}
Step 9: Test Image Generation API
curl -X POST http://localhost:8080/api/images/generate \
-H "Content-Type: application/json" \
-d "{
\"prompt\":\"Create a modern course thumbnail for Spring AI with Java, showing AI chatbot, Java logo style, vector database, and clean blue background\"
}"
Expected Response
{
"imageUrl": "https://..."
}
Complete Request Flow
Client
|
v
POST /api/images/generate
|
v
ImageGenerationController
|
v
ImageGenerationService
|
v
ImageModel
|
v
OpenAI Image Model
|
v
Generated Image URL
Using Image Options
Image generation can usually be controlled with options such as:
- Model
- Size
- Quality
- Style
- Number of images
OpenAI image generation options depend on the selected model. Spring AI’s OpenAI image documentation provides provider-specific configuration details. Spring AI OpenAI Image reference
Example with Image Options Concept
ImagePrompt prompt = new ImagePrompt(
"Create a professional AI course banner for Java developers"
);
ImageResponse response = imageModel.call(prompt);
You can also configure default options through application properties, which is cleaner for most Spring Boot applications.
Prompt Engineering for Image Generation
Image generation quality depends heavily on prompt quality.
Weak Prompt
Create AI image.
Strong Prompt
Create a modern 16:9 course thumbnail for "Spring AI with Java".
Style: clean professional digital illustration.
Elements: Java developer, Spring Boot leaf concept, AI chatbot, vector database nodes.
Background: soft blue gradient.
Mood: premium, educational, trustworthy.
No extra text except title.
Good Image Prompt Structure
Subject:
Style:
Background:
Colors:
Mood:
Composition:
Text:
Restrictions:
Prompt Template for Course Thumbnail
Create a professional course thumbnail.
Course Title: {courseTitle}
Audience: Java developers and students
Style: clean modern educational illustration
Include: AI chatbot, code editor, backend architecture, cloud symbols
Background: premium gradient
Text: {courseTitle}
Avoid: clutter, low-quality icons, unreadable text
Learning Platform Prompt Examples
Spring AI Course Thumbnail
Create a premium course thumbnail for "Spring AI Masterclass".
Show a Java developer building an AI assistant with Spring Boot,
chat bubbles, vector database nodes, and cloud deployment icons.
Use clean modern colors and professional educational style.
RAG Course Thumbnail
Create an educational illustration for "RAG with Java".
Show documents flowing into embeddings, vector database,
and AI chatbot answering questions from knowledge base.
Agentic AI Thumbnail
Create a futuristic but professional image showing an AI agent
planning tasks, calling tools, using memory, and interacting with APIs
in a Java Spring Boot microservices environment.
E-Commerce Prompt Examples
Create a modern promotional banner for an electronics sale.
Include laptop, smartphone, headphones, discount badge, and clean lighting.
Style should be premium, realistic, and suitable for website homepage.
Banking Prompt Examples
Create a simple educational illustration about safe digital banking.
Show a mobile phone, shield icon, UPI payment symbol, and secure transaction flow.
Style should be trustworthy, clean, and professional.
Storing Generated Images
Generated image URLs from providers may not be suitable for permanent storage. In production, download and store generated images in your own storage system if licensing and provider terms allow it.
Storage options:
- AWS S3
- Azure Blob Storage
- Google Cloud Storage
- Local file storage
- Application CDN
Image Storage Flow
Image Generated
|
v
Get Image URL
|
v
Download Image
|
v
Store in S3 / CDN
|
v
Save Final URL in Database
Database Table Example
generated_images
|
+-- id
+-- prompt
+-- provider
+-- model
+-- original_url
+-- stored_url
+-- created_at
+-- user_id
Entity Example
@Entity
@Table(name = "generated_images")
public class GeneratedImage {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(columnDefinition = "TEXT")
private String prompt;
private String provider;
private String model;
@Column(columnDefinition = "TEXT")
private String imageUrl;
private LocalDateTime createdAt;
}
Safety and Moderation
Image generation must include safety checks because users may submit harmful, copyrighted, adult, violent, or policy-violating prompts.
Before calling the image model:
- Validate prompt length
- Block unsafe categories
- Prevent private personal data misuse
- Prevent brand misuse if required
- Prevent identity impersonation
- Log only safe metadata
Prompt Safety Validation Example
public void validatePrompt(String prompt) {
if (prompt == null || prompt.isBlank()) {
throw new IllegalArgumentException("Prompt is required.");
}
if (prompt.length() > 1000) {
throw new IllegalArgumentException("Prompt is too long.");
}
String lower = prompt.toLowerCase();
if (lower.contains("password") || lower.contains("api key")) {
throw new IllegalArgumentException("Prompt contains sensitive terms.");
}
}
Image Generation in Admin Panel
For a learning website, image generation can be added to the admin panel.
Admin enters course title
|
v
Generate prompt automatically
|
v
Call image model
|
v
Preview image
|
v
Admin approves
|
v
Save image to course
Course Image Generation Workflow
Course Title:
Spring AI with Java
System:
Builds professional prompt
Image Model:
Generates thumbnail
Admin:
Reviews and approves
Website:
Displays course image
Auto Prompt Builder for Course Images
public String buildCourseImagePrompt(String courseTitle) {
return """
Create a professional course thumbnail.
Course Title: %s
Style:
Modern, premium, educational, clean digital illustration.
Include:
Java developer, AI assistant, code editor, cloud architecture,
vector database nodes.
Avoid:
Clutter, unreadable text, cartoonish low-quality design.
""".formatted(courseTitle);
}
Image Generation with AI Agents
An AI agent can use image generation as a tool.
User:
Create a course thumbnail for my Spring AI course.
Agent:
1. Understands course topic
2. Builds image prompt
3. Calls image generation tool
4. Returns image preview
5. Stores approved image
Image Generation Tool Example
@Component
public class ImageTools {
private final ImageGenerationService imageService;
public ImageTools(ImageGenerationService imageService) {
this.imageService = imageService;
}
@Tool(description = "Generate an image from a text prompt")
public String generateImage(String prompt) {
return imageService.generateImage(prompt);
}
}
Common Errors and Fixes
1. API Key Missing
Error:
OpenAI API key is not configured
Fix:
export OPENAI_API_KEY=your_key_here
2. ImageModel Bean Not Found
Possible causes:
- Missing Spring AI image/model dependency
- Wrong provider configuration
- API key missing
- Version mismatch
3. Invalid Image Size
Use a size supported by your selected image model.
4. Slow Image Generation
Image generation can take longer than text generation.
Possible fixes:
- Use async processing
- Show loading state in UI
- Store generated results
- Avoid repeated generation for same prompt
5. Bad Image Quality
Improve the prompt by adding:
- Subject
- Style
- Composition
- Lighting
- Background
- Restrictions
Async Image Generation
Image generation may take time. For production, use asynchronous processing.
User Requests Image
|
v
Create Job
|
v
Background Worker Generates Image
|
v
Store Image
|
v
Notify User / Admin
Async Response Example
{
"jobId": "IMG_JOB_1001",
"status": "PROCESSING"
}
Monitoring Image Generation
Track:
- Total image requests
- Success rate
- Failure rate
- Average generation time
- Provider errors
- Cost per image
- Rejected prompt count
- Storage failures
Observability Flow
Image Request
|
+-- Prompt Validation Metric
+-- Provider Latency Metric
+-- Success / Failure Logs
+-- Cost Tracking
|
v
Grafana Dashboard
Security Best Practices
- Never expose API keys in frontend code
- Validate prompt input
- Apply user authentication
- Rate-limit image generation
- Store generated images securely
- Do not log sensitive prompts
- Use admin approval for public website images
- Track generated image ownership
- Follow provider usage policies
Cost Optimization
Image generation can be more expensive than text generation.
Optimization strategies:
- Cache generated images
- Prevent duplicate prompts
- Use admin approval before regenerating
- Limit free users
- Use lower quality for drafts
- Use higher quality only for final assets
- Monitor cost per user
Production Architecture
Frontend / Admin Panel
|
v
Spring Boot Image API
|
+-- Authentication
+-- Prompt Validation
+-- ImageModel
+-- Storage Service
+-- Metadata Database
+-- Monitoring
|
v
Image Provider
Best Practices
- Use strong image prompts
- Keep prompts specific and descriptive
- Validate prompts before generation
- Use environment variables for API keys
- Store generated images in your own storage if needed
- Use async jobs for production
- Rate-limit expensive image generation
- Track provider cost and failures
- Use admin review before publishing images
- Follow image provider safety and usage policies
Interview Questions
Q1: What is image generation in Spring AI?
Image generation in Spring AI means using the ImageModel API to generate images from text prompts through supported AI image providers.
Q2: What is ImageModel?
ImageModel is the Spring AI abstraction used to call image generation models.
Q3: What is ImagePrompt?
ImagePrompt represents the text prompt and options sent to an image model.
Q4: Why should image generation be rate-limited?
Because image generation can be expensive and resource-intensive, and rate limiting prevents abuse.
Q5: Why should generated images be stored separately?
Provider-generated URLs may not always be permanent, so production systems often store approved images in their own storage or CDN.
Advanced Interview Questions
Q1: How do you make image generation production-ready?
Use prompt validation, secure API keys, async processing, storage, rate limiting, monitoring, cost tracking, and admin approval workflows.
Q2: How do you improve image generation quality?
Use detailed prompts with subject, style, composition, background, lighting, mood, and restrictions.
Q3: How can image generation be used with AI agents?
Image generation can be exposed as a tool so an AI agent can create thumbnails, banners, diagrams, or visual assets when requested.
Q4: What should be monitored in image generation systems?
Request count, success rate, provider latency, failures, rejected prompts, storage errors, and cost per image.
Q5: Why is prompt safety important for image generation?
Because users may submit unsafe, sensitive, misleading, or policy-violating prompts, so validation and moderation are required.
Recommended Learning Path
- Introduction to Spring AI
- Chat Models and ChatClient
- Prompt Engineering
- Structured Outputs
- Function Calling and Tool Integration
- Building AI Agents with Spring AI
- Image Generation with Spring AI
Summary
Image Generation with Spring AI allows Java developers to build applications that generate images from text prompts using a clean Spring Boot-friendly API.
Spring AI provides the ImageModel, ImagePrompt, and ImageResponse abstractions for working with image generation models. OpenAI image generation can be configured through Spring AI provider properties, including the OpenAI API key and model options.
For real-world applications such as learning platforms, e-commerce websites, banking education tools, blog platforms, and admin panels, image generation can help create thumbnails, banners, illustrations, diagrams, and visual assets.
Production image generation should include prompt validation, secure API key handling, rate limiting, async processing, storage, monitoring, cost control, and review workflows before publishing generated images publicly.