Spring Boot Guide for Beginner: Hello World
Get your Spring Boot project running with this easy set up
There are a lot of frameworks for you to build a web application; one of them is Spring Boot. This article will be a step-by-step guide for you to learn the basic setup of this excellent framework. Spring Boot is quite popular, so learning it will put you in a good position.
Spring is one of the popular frameworks that you can use to build production-grade applications. Based on a survey by Stack Overflow in 2021, Spring is listed as the ten most used framework by developers. One of the substantial benefits that Spring can offer is the dependency injection feature. This feature let you reduce the amount of coupling in your code so that it can be modular.
Spring Boot was built on top of Spring Framework, making it easy for developers to use it. Spring Boot help you create a web application with minor to no configuration. This framework takes an opinionated approach to get you to develop your product faster with less boilerplate code. An opinionated framework means you will have an established application structure when you first initiate your project. Another web framework that took this approach is Ruby on Rails, Django, and Laravel.
Prerequisite
To be able to use this guide, you will require to install a few dependencies first. But don’t worry, I put an external link for the manual for installing them. You’ll need:
Spring Initializer
With Spring Boot, you can also customize your starter code so that there are no unused code. This customization can be done with just a few simple click with Spring Initializr. For this guide, you will need to add Spring Web as its dependencies and use Java 8. Feel free to modify project metadata to be your own. You can access Spring Initializr tool by the link here.
After setting up the Java version and project dependencies, click generate button to compile the starter code and then it automatically download to your computer. Unzip the starter code and open the folder with your favourite code editor or IDE to get a deep dive into Spring Boot.
Dive into Spring Boot
When you first open the project folder, there were many documents already set up for you. Spring Boot has a clearly established structure so that you just put more effort into writing your business logic. As a beginner in Spring Boot, you will mainly be working in the ./src
folder. In that folder, you will find two folders: main
and test
. Production code will be written in the main
folder; meanwhile, the unit tests will fill the test
folder.
For this simplicity of guide, you will only modify DemoApplication.java
file located in ./src/main/java/com/example/demo/
. Follow these steps below to modify our starter code to be able to print hello world to our client:
- Add
@RestController
annotation toDemoApplication
class - Implement a new function that returns the string “Hello, World!”
- Add
@GetMapping
annotation into the newly implemented function. - Run the application
The final modification should look like the code provided below. It is okay to improvise as long as you can run the application without any errors.
In this guide you already see @RestController
and @GetMapping
annotations. You will see a lot of annotation in the Spring Boot framework. But, don’t get overwhelmed; over time, you will understand what each of these annotations is for.
@RestController
is an annotation that tells Spring Boot that this class is a controller component and can handle HTTP Requests.- A function that has
@GetMapping
means that that function is a handler for GET requests. This annotation should be inside a class that has@RestController
annotation to work correctly. Another variation for this annotation is@PostMapping
,@DeleteMapping
, and@PutMapping
.
Result
If you managed to compile the project without any errors, you would be rewarded by seeing this output in the address localhost:8080
. Congratulations! You have taken a first step in mastering this framework.
Further reading
There are still a lot to learn in order to master Spring Boot. I have compiled learning source for you to read below:
- Securing applications with JWT Spring Boot by Ignacio Oliveto
- Spring Data JPA-The Overview by Tranh Tran
- Building a Microservice with Spring Boot and Spring Cloud
Special shout out to Amigoscode. His great and easy to understand content really help me into understanding Spring Boot. Please go check his Youtube channel and don’t forget to subscribe.
Thank you!