Open In App

Utilizing Text-to-Image Models with Spring AI

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Artificial Intelligence (AI) has made significant strides in recent years, revolutionizing various aspects of everyday life. Whether for entertainment, education, or business, AI tools are now widely accessible. Developers and technologists are leveraging AI to create content, generate code templates, debug programs, and more. One of the most exciting advancements is text-to-image models, which can produce stunning visuals from simple text descriptions. These models enable users to generate visual content precisely tailored to their needs.

Popular platforms such as OpenAI's DALL-E and Midjourney have gained immense popularity for their ability to generate images based on text prompts. Integrating such AI capabilities into Java Spring applications offers robust deployment opportunities in real-world applications. With Java's robustness and versatility, incorporating text-to-image models using Spring AI becomes feasible and efficient.

In this article, we will guide you through implementing a text-to-image model using Spring AI, focusing on setup, configuration, and generating images from text-based prompts.

Why Spring AI?

Spring AI provides a comprehensive ecosystem for integrating AI capabilities into Java applications. With seamless integration with OpenAI APIs, developers can harness AI's potential to generate images from text descriptions without delving into the complexities of AI models. It abstracts away much of the low-level details, making it easier for developers to focus on building their applications.

Core Concept of Text-to-Image Generator Models

Text-to-image models are advanced deep learning models trained on vast datasets containing images paired with textual descriptions. These models learn to map semantic features from the text to corresponding visual elements in the images.

These models often use Transformer architectures like GPT to understand the text, while models like Generative Adversarial Networks (GANs) or Diffusion Models generate the corresponding image. By utilizing Spring AI, developers can build APIs or microservices that enable text-to-image generation, making it easier to integrate AI-driven image generation into platforms, mobile apps, or web interfaces.

Pre-requisites for Implementation:

Before diving into the technical implementation, ensure that you meet the following prerequisites:

  • Java Development Kit (JDK), version 11 or above.
  • An OpenAI Account to generate an API key.
  • Spring Boot: Version 2.6 or above.
  • A development environment such as IntelliJ IDEA or Spring Tool Suite.
  • Familiarity with Maven as a build tool.
  • An API testing tool like Postman to test your API endpoints.

Project Structure:

The project will follow a typical Spring Boot structure with necessary dependencies added to enable image generation via OpenAI.

Project Folder Structure


Let's go step-by-step for the configuration of Spring AI to generate Image from text.

Step-by-Step Implementation to Utilize Text-to-Image Models with Spring AI

Step 1: Create a New Spring Boot Project

Start by creating a new Spring Boot project using Spring Initializer or any preferred method (Spring Tool Suite, IntelliJ, etc.). Be sure to include the following dependencies in the pom.xml file:

  • Spring Web: For building REST APIs.
  • Spring Boot Devtools: For hot reload.
  • Jackson Databind: For handling JSON data.
  • Lombok: To simplify Java code (optional).
Project Metadata

Step 2 : Add Dependencies

Open the pom.xml file and must include the following dependencies.

<dependencies>
<!-- Spring Boot Web dependency for creating REST APIs -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Dependency for consuming external APIs -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- Jackson Databind for handling JSON responses -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- Lombok for simplifying Java code -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

pom.xml File View:

pom.xml file

Step 3: Configure the OpenAI API Key

Next, configure your OpenAI API key in the application.properties file to securely communicate with OpenAI’s API.

spring.application.name=springaiimagegenerator
spring.ai.openai.api-key=YOUR_OPENAI_API_KEY

application.properties file view:

application.properties file

Step 4: Create the Image Generation Controller

Now, create a REST controller that exposes an API endpoint to receive a text prompt and return the generated image URL. This will be responsible for communicating with OpenAI’s API and returning the image result.

path: " src/main/java/com/example/springaiimagegenerator/controller/ImageGeneratorController.java "

Java
// ImageGeneratorController.java

package com.example.springaiimagegenerator;

import org.springframework.ai.image.Image;
import org.springframework.ai.image.ImageClient;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ImageGeneratorController {


     @Autowired
     private ImageClient openAiImageClient;

    @GetMapping("/image/{imagePrompt}")
    public Image getImage(@PathVariable String imagePrompt){
        ImageResponse response = openAiImageClient.call(
                new ImagePrompt(imagePrompt,
                        OpenAiImageOptions.builder()
                                .withQuality("hd")
                                .withN(4)
                                .withHeight(1024)
                                .withWidth(1024).build())

        );
        return response.getResult().getOutput();
    }
}


This controller creates an API endpoint at /image/{imagePrompt} to accept text descriptions and return an image in response.

Step 5: Configure the Image Client in the Main Application

In the main class, configure the ImageClient bean that will interact with the OpenAI API to generate images.

Java
// SpringAIIamgeGeneratorApplication.java

package com.example.springaiimagegenerator;

import org.springframework.ai.image.ImageClient;
import org.springframework.ai.openai.OpenAiImageClient;
import org.springframework.ai.openai.api.OpenAiImageApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringaiimagegeneratorApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringaiimagegeneratorApplication.class, args);
	}
	
    // Image Client Configuration 
	@Bean
	ImageClient imageClient(@Value("${spring.ai.openai.api-key}") String apiKey) {
		return new OpenAiImageClient(new OpenAiImageApi(apiKey));
	}

}


This configuration ensures the ImageClient is ready to interact with OpenAI’s API.

Step 6: Create the Record file for Handling the API request

In the record file ImageGenRequest.java, it provides easiness to handling the API endpoint request.

Java
// ImageGenRequest.java

package com.example.springaiimagegenerator;

public record ImageGenRequest(String prompt) {

}

Step 7: Testing the API Using Postman

Once the project is configured, it’s time to test your API. Follow these steps:

  • Run the Spring Boot application.
  • Open Postman and send a GET request to http://localhost:8080/image/{your-prompt} where {your-prompt} is the text description of the image you want to generate.
  • The response will return a JSON object containing the URL of the generated image.
  • Click the URL to preview the generated image in your browser.


Also you can modify the above controller file, as per your requirement.

  • If you want to replace the pathVariable with RequestParam then you can modify it accordingly.
  • Then you pass the prompt into the BODY of Postman. Then select the raw and then JSON, provide the prompt in it like below:
// if You use Request Param
// then pass the prompt into body section of Postman in JSON Form

{
"prompt" : " A spaceship flying through a nebula "
}


Postman API Testing:

Postman UI

Output:

Here is an example prompt and result:

  • Prompt: "give me an image of dog"
  • Output: A URL that opens the generated image of dog.
Generated Image

Conclusion

By following this article, you can integrate powerful text-to-image capabilities into your Spring Boot applications using Spring AI, providing a dynamic way to generate images from text prompts.


Next Article
Article Tags :

Similar Reads