eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

Artificial Intelligence is changing the way we build web applications. One exciting application of AI is generating an image from a text-based description. OpenAI’s DALL·E 3 is a popular text-to-image model that helps us achieve this.

In this tutorial, we’ll explore how to generate images with OpenAI’s DALL·E 3 model using Spring AI.

To follow this tutorial, we’ll need an OpenAI API key.

2. Setting up the Project

Before we can start generating AI images, we’ll need to include a Spring Boot starter dependency and configure our application correctly.

2.1. Dependencies

Let’s start by adding the spring-ai-openai-spring-boot-starter dependency to our project’s pom.xml file:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
    <version>1.0.0-M3</version>
</dependency>
Since the current version, 1.0.0-M3, is a milestone release, we’ll also need to add the Spring Milestones repository to our pom.xml:
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

This repository is where milestone versions are published, as opposed to the standard Maven Central repository.

The above starter dependency provides us with the necessary classes to interact with the OpenAI service from our application and generate AI images using its DALL·E 3 model.

2.2. Configuring an OpenAI API Key

Now, to interact with the OpenAI service, we need to configure our API key in our application.yaml file:

spring:
  ai:
    openai:
      api-key: ${OPENAI_API_KEY}

We use the ${} property placeholder to load the value of our property from an environment variable.

On configuring a valid API key, Spring AI will automatically create an ImageModel bean for us. We’ll autowire it in our service layer and send requests to generate AI images.

2.3. Configuring Default Image Options

Next, let’s also configure a few default image options that’ll be used to generate our images:

spring:
  ai:
    openai:
      image:
        options:
          model: dall-e-3
          size: 1024x1024
          style: vivid
          quality: standard
          response-format: url

We first configure dall-e-3 as the model to use for image generation.

To get a perfect square image, we specify 1024×1024 as the size. The other 2 allowed size options are 1792×1024 and 1024×1792.

Next, we set the style to be vivid — that tells the AI model to generate hyper-realistic and dramatic images. The other available option is natural, which we can use to generate more natural and less hyper-realistic images.

For the quality option, we set it to standard, which will work for most use cases. However, if we need images with enhanced details and better consistency, we can set the value to hd. It should be noted that images of hd quality will take more time to generate.

Finally, we set the response-format option to url. The generated image will be accessible via a URL of 60-minute validity. Alternatively, we can set its value to b64_json to receive the image as a Base64-encoded string.

We’ll look at how to override these default image options later in the tutorial.

3. Generating AI Images With DALL·E 3

Now that we’ve set up our application, let’s create an ImageGenerator class. We’ll autowire the ImageModel bean and reference it to generate AI images:

public String generate(String prompt) {
    ImagePrompt imagePrompt = new ImagePrompt(prompt);
    ImageResponse imageResponse = imageModel.call(imagePrompt);
    return resolveImageContent(imageResponse);
}

private String resolveImageContent(ImageResponse imageResponse) {
    Image image = imageResponse.getResult().getOutput();
    return Optional
      .ofNullable(image.getUrl())
      .orElseGet(image::getB64Json);
}

Here, we create a generate() method which takes a prompt String, representing the text description of the image we want to generate.

Next, we create an ImagePrompt object with our prompt parameter. Then, we pass it to the call() method of our ImageModel bean to send the image generation request.

The imageResponse will contain either a URL or a Base64-encoded String representation of the image, depending on the response-format option we configured earlier in our application.yaml file. To resolve the correct output property, we create a resolveImageContent() helper method and return it as a response.

4. Overriding Default Image Options

In some cases, we may want to override the default image options we configured in our application.yaml file.

Let’s take a look at how we can do this by overloading our generate() method:

public String generate(ImageGenerationRequest request) {
    ImageOptions imageOptions = OpenAiImageOptions
      .builder()
      .withUser(request.username())
      .withHeight(request.height())
      .withWidth(request.width())
      .build();
    ImagePrompt imagePrompt = new ImagePrompt(request.prompt(), imageOptions);
    
    ImageResponse imageResponse = imageModel.call(imagePrompt);
    return resolveImageContent(imageResponse);
}

record ImageGenerationRequest(
    String prompt,
    String username,
    Integer height,
    Integer width
) {}

We first create an ImageGenerationRequest record, which, in addition to holding our prompt, contains the username and the desired image height and width.

We use these additional values to create an ImageOptions instance and pass it to the ImagePrompt constructor. It’s important to note that the OpenAiImageOptions class doesn’t have a size property, hence we provide the height and width values separately.

The user option helps us link the image generation request with a specific end user. This is recommended as a security best practice to prevent abuse.

As per requirement, we can also override other image options like style, quality, and response-format in our ImageOptions object.

5. Putting Our ImageGenerator Class to the Test

Now that we’ve implemented our ImageGenerator class, let’s test it out:

String prompt = """
    A cartoon depicting a gangster donkey wearing 
    sunglasses and eating grapes in a city street.
""";
String response = imageGenerator.generate(prompt);

Here, we pass our prompt to our ImageGenerator’s generate() method. After a short processing time, we’ll receive a response containing the URL or Base64-encoded string of our generated image, depending on the configured response-format property.

Let’s take a look at what DALL·E 3 generated for us:

A cartoon depicting a gangster donkey wearing sunglasses and eating grapes in a city street.

As we can see, the generated image accurately matches our prompt. This demonstrates the power of DALL·E 3 in understanding natural language descriptions and turning them into an image.

6. Conclusion

In this article, we explored how to generate AI images from textual description using Spring AI. We used OpenAI’s DALL·E 3 model under the hood.

We walked through the necessary configurations and developed a service class to generate AI images. Additionally, we looked at the default image options and how to override them dynamically.

By integrating DALL·E 3 into our Java applications via Spring AI, we can easily add image generation capabilities without the overhead of training and hosting our own models.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments