Booting with Spring Boot
Create a project in 10 minutes or less!
--
This is a step-by-step guide for generating your first Spring Boot application.
Spring Boot is a Java framework (built on top of the Spring framework) that makes it easy to build a production ready application. Spring Boot favors convention over configuration, making Spring development efficient and approachable. With Spring Boot, developers do not have to make every little decision about infrastructure or default behaviors. A new project comes configured with commonly used dependencies and default properties. Plus, initial configuration can also be easily overridden if needed.
If you are familiar with Ruby, Spring Boot is to Java as Rails is to Ruby. Ruby is an open-source, object-oriented programming language while Rails is a web application framework written in Ruby. Java is an object-oriented programming language while Spring Boot is a Java framework. Similar to Spring Boot, Rails adopts the convention over configuration concept, with a series of best practices. Every Rails project has the same MVC structure and comes with built-in modules such as Action Pack, Active Model, etc. Both Spring Boot and Rails require minimal configuration, so developers can focus on what they do best — CODE!
STEP 1: Install Java
According to the Spring Boot docs, Spring Boot 2.5.0 requires Java 8, but is compatible up to and including Java 16. Before installing, check if Java is installed.
java -version
If installed, the version, runtime, and server information will be displayed.
openjdk version "16.0.1" 2021-04-20OpenJDK Runtime Environment (build 16.0.1+9-24)OpenJDK 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)
If Java is not installed and you have Homebrew, simply type the following commands in your terminal.
brew updatebrew install java
If not, follow the simple Java download instructions.
STEP 2: Download IntelliJ IDEA
If you haven’t been developing in Java, you will also need to download IntelliJ, the IDE for Java. Before downloading, you will need to select either the ‘Ultimate’ or ‘Community’ edition. IntelliJ offers a free 30-day trial of the Ultimate edition, so I decided to give it a try. However, the Community edition works great as well.
STEP 3: Create your project using spring initializr
To get started with Spring Boot, navigate to spring initializr. Spring initializr helps you create a new Spring Boot Project quickly.
You will then make a few quick selections, noted below.
I kept most of the default options for simplicity.
Project: Maven or Gradle are the tool options for building a project (I chose Maven).
Language: Java, Kotlin, or Groovy (I chose Java).
Spring Boot: Select a Spring Boot version (I chose 2.5.0).
Project Metadata: Add a name and/or description for the project (demo) and select the packaging (Jar) and Java version (16).
Dependencies: Add Spring Web, which uses Spring MVC, REST, and Tomcat as the embedded default server.
Then, click ‘GENERATE.’
This will create a new project, which you can access as a zip file in your downloads. Double click on the zip file, which will create a folder with the same name as what you specified in the spring initializr (demo).
STEP 4: Open project in IntelliJ IDEA
Open IntelliJ. You will be prompted to create a new project or open. Click ‘open’ and select the folder with your project name. It will take a few moments to load the project.
STEP 5: Start the Server
Inside the demo folder, navigate to
src/main/java/com.example.demo/DemoApplication
You can run the application here by clicking on the green arrow. In addition, you can right click and select ‘Run DemoApplication.’ A series of prompts will be shown in the terminal, indicating the web server is up and running on localhost:8080.
If we try to navigate to localhost:8080, we get a 404 error because we have not implemented any endpoints.
STEP 6: Implement an API
In IntelliJ, stop the server by clicking on one of the red boxes (either at the top, side, or bottom of the page).
Inside of DemoApplication, add
@RestController
which indicates this is a RESTful controller.
In addition, add the method hello() which prints “Hello World.” Add @GetMapping above the method to indicate this is a RESTful endpoint.
@GetMapping
public String hello() {
return "Hello World";
}
Start the server and navigate to localhost:8080, to see “Hello World.”
And voila, a fully-functioning web API using Spring Boot!
I hope you feel more comfortable with Spring Boot. As you’ve seen in this tutorial, Spring boot makes it fast and easy to create a new project.
References:
Amigoscode: https://www.youtube.com/watch?v=9SGDpanrc8U&t=3536s
Telusko: https://www.youtube.com/watch?v=35EQXmHKZYs
Spring initializr: https://start.spring.io/