This repository provides a practical implementation of integrating Spring AI with Java microservices, demonstrating how to leverage the power of OpenAI's ChatGPT API within modern Spring-based applications. Designed as a step-by-step guide, it showcases best practices and up-to-date techniques for enhancing microservices with AI capabilities.
- Java (main language)
- Spring Boot (microservices framework)
- Spring AI (AI integration library)
- OpenAI API (ChatGPT integration)
- Maven (project management and build tool)
- Java 21 or higher installed
- Maven 3.8+
- OpenAI API key (sign up at OpenAI and obtain your API key)
-
Clone the repository:
git clone https://github.com/Intercont/spring-ai-intro.git cd spring-ai-intro
-
Configure your OpenAI API Key
- Create or edit the file
src/main/resources/application.properties
and add:spring.ai.openai.api-key=YOUR_OPENAI_API_KEY
- It is recommended to set the API key as an environment variable instead of having it on your properties file:
export OPENAI_API_KEY=YOUR_OPENAI_API_KEY
- Create or edit the file
-
Build the project
mvn clean package
-
Run the application
mvn spring-boot:run
or, using the generated JAR:
java -jar target/spring-ai-intro-*.jar
src/main/java/
: Java source codesrc/main/resources/
: Configuration filespom.xml
: Maven build configuration
- Step-by-step integration of Spring AI with Java microservices
- Communication with OpenAI’s ChatGPT API
- Example endpoints and request workflows
- Easily extensible for custom AI use cases
Contributions are welcome! Please open an issue or submit a pull request for suggestions, bug fixes, or enhancements.
This project is licensed under the MIT License.
Explore how to empower your Java microservices with AI using Spring AI and OpenAI!
Subscribe at igorfragadev.com for more