Spring Boot project can be created using the following options,
- Spring Tool Suite (STS) Integrated Development Environment (IDE).
- Spring Initializr website.
- Maven project.
- Spring Boot CLI
The easiest option out of all the above is to use the Spring Initializr website to get the starter POM. The Spring Initializr website will provide a user friendly option to select the required dependencies and project configurations. To setup your first spring boot project, visit https://start.spring.io.
Select the options as shown in the above image (which are the default options available in the website). You can provide a name to your project package and the artifact. By clicking the ‘Switch back to the simple version.’, you will be able to view the entire list of dependencies from which you can select the required one. Alternatively you can use the search box displayed in the right side of the page to select the dependencies. In the above image, we have selected Web and JPA (Java Persistence API) dependencies.
After selecting the required options, click the ‘Generate Project‘ button (the big green button) or you can press the shortcut key combination ‘Alt + Enter’ to do the same. The generated starter project will be downloaded in the ZIP format with the name mentioned in the ‘Name’ field. The downloaded ZIP file will contain the project files and also the starter POM which will readily help you to get started with the application. Following is the generated POM file which contains all information needed to bootstrap the project.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.codetreat</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Importing the project into the IDE
Download the latest version of Spring Tool Suite (STS) IDE from the STS website https://spring.io/tools/sts/. The STS IDE has been built on top of the open source Eclipse IDE with added plugins and features like AOP support, code assist for Spring APIs, easy configuration setup etc. that makes it easy for the developers to get started with a spring project instantly. Please follow this tutorial to finish the installation of STS. After the installation, open STS and import the downloaded Spring project by following the below steps.
- Click File -> Import.
- In the Import Wizard search box, enter the keyword Maven and select Existing Maven Projects from the search result and click Next.
- Click the Browse button and enter the path of the extracted folder from the ZIP file which was downloaded from the Spring Initializr website. The pom.xml file of the project will be displayed in the Projects section after successfully importing it.
- Click the Finish button.
The Maven dependencies will be downloaded in the background and the project should not display any errors in the Error Log section of the IDE as shown below. Now that the bootstrapping has been done successfully, we will create a simple ‘Hello World’ app to see how easy is it to get started with the Spring Boot.