A small Spring Boot application that uses Spring AI and OpenAI models for two image workflows:
generate a PNG from a text prompt with /image, or upload an image to /vision and receive a
detailed description of its contents.
- Java 21
- An OpenAI API key with access to image generation and vision-capable chat models
The application reads the API key from the OPENAI_API_KEY environment variable. Export it before
starting the application:
export OPENAI_API_KEY=your-api-keyStart the application with the Maven wrapper:
./mvnw spring-boot:runThe application listens on http://localhost:8080.
With the application running, copy and run this command in a second terminal. The response is a PNG image, so redirect it to a file:
curl --location 'http://localhost:8080/image' \
--header 'Content-Type: application/json' \
--data '{
"question": "The Last JVM Engineer: a dramatic and humorous cinematic poster in which a lone Java developer stands on the roof of a server fortress during a thunderstorm, wielding a glowing golden JVM power core while an army of rogue AI-generated bugs rises from broken build pipelines. A sarcastic robot code reviewer in a hoodie leads the bugs, holding a red warning triangle like a battle standard. Behind the developer, an enormous neural network forms a protective halo around a coffee-fueled CI/CD pipeline. The developer deploys one final clean build, turning every bug into helpful unit tests. Epic scale, photorealistic detail, dramatic blue-versus-amber lighting, futuristic software engineering atmosphere, expressive characters, subtle developer humor, vertical theatrical poster composition, no readable text, no logos, no watermarks."
}' \
--output java-ai-poster.pngOpen output/java-ai-poster.png after the request completes.
This prompt creates a tense, darkly humorous Java-and-AI survival scene:
curl --location 'http://localhost:8080/image' \
--header 'Content-Type: application/json' \
--data '{
"question": "The Nightly Build After the Outbreak: a cinematic survival-horror scene in a deserted city reclaimed by nature, where a determined Java developer guards the last functioning data center against a massive horde of AI-generated code zombies. The undead wear torn office clothes and carry broken keyboards, empty coffee cups, and obsolete bug reports. A small service robot projects a glowing blue Java stack trace onto the fog as a warning signal, while an improvised team of developers fortifies the server room with Ethernet cables and deploys automated tests as defensive drones. Moody overcast sky, rain-slick streets, ominous atmosphere, dramatic flashlight beams, realistic practical details, tense survival storytelling, dark developer humor, no readable text, no logos, no watermarks."
}' \
--output java-zombie-apocalypse.pngThis example is designed for a vivid, photorealistic Copacabana beach-volleyball scene:
curl --location 'http://localhost:8080/image' \
--header 'Content-Type: application/json' \
--data '{
"question": "A breathtaking photorealistic sports-editorial photograph on Copacabana Beach in Rio de Janeiro at golden hour, featuring three confident adult women in tasteful athletic beachwear playing an energetic beach-volleyball rally on a professional sand court, one athlete leaping for a spectacular overhead hit while her teammates move into position and laugh naturally. Make Copacabana unmistakable: the sweeping crescent shoreline, busy promenade, black-and-white Portuguese wave mosaic pavement, rows of palm trees, colorful beach kiosks, beachgoers, and the Christ the Redeemer statue clearly visible on the mountain ridge in the background, with Sugarloaf Mountain farther across the bay. Warm low sunlight, ocean spray and sand particles frozen in the air, authentic skin texture, detailed hair and fabric, realistic athletic motion, natural anatomy, cinematic depth, vibrant but true-to-life colors, high-end travel and sports magazine composition, 85mm lens, no readable text, no logos, no watermark."
}' \
--output rio-beach-ai.pngRequest body:
{
"question": "A detailed description of the image to generate"
}The endpoint returns the generated image as image/png. The current implementation uses the
gpt-image-1 model, requests a 1024x1024 image at high quality, and describes the desired
photorealistic appearance in the prompt. GPT Image models return image data as Base64 by default;
the application decodes it before sending the PNG to the client.
Upload a JPEG image as the file multipart form field to receive an AI-generated description.
The vision workflow uses the gpt-4o-mini model.
curl --location 'http://localhost:8080/vision' \
--header 'Content-Type: multipart/form-data' \
--form 'file=@"/path/to/image.jpg"'In Postman, replace /path/to/image.jpg with the image file selected in the form-data editor.
The postman-cloud:///... file reference is specific to Postman and is not portable to a
terminal outside that application.
Example response:
AssistantMessage [messageType=ASSISTANT, toolCalls=[], textContent=The image is a vibrant,
intricate illustration that features a multitude of elements arranged in a circular pattern.
The color scheme predominantly includes red and blue against a light background, creating a
striking contrast. In the center, there appears to be a stylized number "250", surrounded by
doodles representing technology, music, transportation, people, and creativity.]
./mvnw testThis repository contains source code examples used to support online courses about the Spring Framework.
- Spring Framework 6 - Beginner to Guru
- Hibernate and Spring Data JPA: Beginner to Guru
- API First Engineering with Spring Boot
- Introduction to Kafka with Spring Boot
- Spring Security: Beginner to Guru
- Spring Framework 5: Beginner to Guru - Get the most modern and comprehensive course available for the Spring Framework! Join over 17,200 over Guru's in an Slack community exclusive to this course! More than 5,700 students have given this 53 hour course a 5 star review!
- Spring Boot Microservices with Spring Cloud Beginner to Guru - Master Microservice Architectures Using Spring Boot 2 and Cloud Based Deployments with Spring Cloud and Docker
- Reactive Programming with Spring Framework 5 - Keep your skills razor sharp and take a deep dive into Reactive Programming!
- Testing Spring Boot: Beginner to Guru - ** Best Selling Course** Become an expert in testing Java and Spring Applications with JUnit 5, Mockito and much more!
- SQL Beginner to Guru: MySQL Edition - SQL is a fundamental must have skill, which employers are looking for. Learn to master SQL on MySQL, the worlds most popular database!
- Apache Maven: Beginner to Guru - Best Selling Course Take the mystery out of Apache Maven. Learn how to use Maven to build your Java and Spring Boot projects!
- OpenAPI: Beginner to Guru - Master OpenAPI (formerly Swagger) to Create Specifications for Your APIs
- OpenAPI: Specification With Redocly
- Docker for Java Developers - Best Selling Course on Udemy! Learn how you can supercharge your development by leveraging Docker. Collaborate with other students in a Slack community exclusive to the course!
- Spring Framework DevOps on AWS - Learn how to build and deploy Spring applications on Amazon AWS!
- Ready for Production with Spring Boot Actuator - Learn how to leverage Spring Boot Actuator to monitor your applications running in production.
- Mastering Thymeleaf with Spring Boot - Once you learn Thymeleaf, you'll never want to go back to using JSPs for web development!


