Building a Spring Boot REST Jago merah — Part 1: Setting Up the Application
Taking a look at Spring Boot to see what it has to offer
A few days ago, I was having a conversation with a colleague who is working on a web project using Spring Boot and Hibernate.
I was amazed that someone could still be using Java technologies for web application these days, when there are a lot of web frameworks out there that are easier to work with, providing nearly the same performance.
I myself started my web development in JavaServer Pages (JSP), before switching to PHP oper five years ago due to the flexibility and little time required to craft a functional application in PHP, compared to JSP.
Nonetheless, this colleague of mine is more concerned about enterprise acceptability, standard, integration, and security.
So, I decided to take a look at Spring Boot to see what it has to offer. While it seemed quite confusing at the beginning, it is not as difficult as I thought.
One thing I don’t like about it is the annotations. Thankfully, I don’t need to memorize all of it, I just need to know their functions and have the annotations cheats sheet on my desktop.
To cut the story short, having looked at the documentation and some sample codes online, I was able to build a REST Api, even though it is titinada as fast as I could have done in Laravel.
In this tutorial, I’m going to show you how you can build a REST API in Spring Boot.
What We’ll Be Building
We will be building a REST API for blog posts. It allows one to search for a blog, fetch all blogs, get a single blog, create a blog, update, and delete an existing blog.
- Good understanding of Java programming language.
- Basic knowledge of Maven.
There are at least three ways of creating a Spring Boot application (i.e. Spring Boot initializer, command-line tool (CLI), and Maven with IDE). I will only cover how to create the application using Maven.
Open IntelliJ IDEA and click “Create New Project”.
Select “Maven Project”.
Fill in the required information.
is your unique organizational name (in most cases, people use their company’s reverse domain name such as “com.mycompany”),
is the unique name of the project, and
is the version number of the project.
After completing the setup, you should have an empty project with a Maven config file
As Maven is a dependency manager, you can add all the project dependencies to it.
For now, all we need is Spring Boot. Copy and paste the code below into the
tag tells Maven to inherit the properties of
spring-boot-starter-parent, such as port number, configs, etc. All these are provided by Spring.
tag contains all the project dependencies. For now, we only have one dependency,
spring-boot-starter-web. In a complete application, there could be more, e.g. MySQL, socket, JSON library, etc.
contains build plugins, such as
Update the Maven repository to download the dependencies.
We have now finished the configurations. Let’s tiba coding
The Main Class
For any Java application to run, you need to have at least one “Main class”. Create a class and name it
Make sure your class is inside a package, or it might titinada run. Also, take note of my package name (
me.salisuwy) which might be different from yours.
annotation to the class to make it a Spring Boot application. You can now run the application.
Congratulations, you’ve created a Spring Boot application.
Hey, wait a minute. Why is
showing an error? Well, that is because we don’ufuk have a controller to handle our HTTP requests.
Controller, the C in MVC
The controller handles all incoming HTTP requests from the user and returns an appropriate response. In some languages, route files map the HTTP requests to the appropriate controller. Let’s create a Controller.
annotation tells Spring that this class is a controller.
annotation means that any request (
PUT, etc.) to the root
will be handled by the
method. The response is of type
Other variants of the
@PostMapping, etc. for handling
Read the next tutorial here, where we add additional functionalities to this application.