The first chapter of our guide on Spring AI could only be about how to create a project from scratch. We will see together the steps to take and do a quick analysis of the code to understand the basics of how the framework works.
Spring Initializr
The simplest method to create a Spring from scratch project is certainly to use the Spring Initializr tool available online. Thanks to this tool, developers can define project dependencies, select the Spring version and specify other configuration options, such as the programming language (for example Java or Kotlin), the dependency management system (for example Maven or Gradle), and so on. Once the configuration is complete, the Spring Initializr automatically generates the basic structure of the project, including the necessary configuration files and dependencies, ready to be imported and used in an integrated development environment (IDE) such as IntelliJ IDEA or Eclipse.
For our tutorial we will use Maven dome dependency management tool and import the Spring AI extension to use the Open AI (GPT) model.
Project dependencies
Once the project has been generated using the online tool, it is possible to download it locally and open it with any IDE. In our example we will use Eclipse.
The first thing to do is definitely take a look at the pom.xml file where you will immediately notice that the dependency on the Spring AI BOM has been set.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
The BOM, an acronym for “Bill of Materials”, is a document that lists all the components and materials necessary for the production of a specific product. In essence, it is a structured and organized list of the components and dependencies needed to use Spring AI within a project. By importing the BOM, all dependencies will be automatically imported and managed by the dependency management system (such as Maven or Gradle), avoiding the need to manually specify each dependency separately.
In order for Maven to download the necessary dependencies, it also needs the reference to the repository to be set:
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
Last thing to note, the dependency on the Spring AI module for integration with Open AI has also been automatically imported.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
Of course, in addition to what is described above, the pom also contains all the classic dependencies of a Spring Boot project but we will not analyze them in detail as they are not the subject of our tutorial.
The Application class
As in all Spring Boot applications, the Application class contains the main method and is the starting point of our application.
package com.example.aidemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AidemoApplication {
public static void main(String[] args) {
SpringApplication.run(AidemoApplication.class, args);
}
}
Trying to start the application will raise the error “Web application could not be started as there was no org.springframework.boot.web.reactive.server.ReactiveWebServerFactory bean defined in the context.” which can be solved by inserting the Spring Web module among the dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
At this point our application with Spring AI is ready and running! In the next chapter of the guide we will see how to integrate the app with the OpenAI API.