Building a Spring Boot REST API — Part 1: Setting Up the Application
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.
If you want to Gain In-depth Knowledge on Spring Boot, please go through this link Spring Boot Online Training
I myself started my web development in JavaServer Pages (JSP), before switching to PHP over 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 not 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.
Requirements
- Good understanding of Java programming language.
- Basic knowledge of Maven.
Tools Needed
- Java Development Kit (JDK) 1.7+
- IntelliJ IDEA, NetBeans, Eclipse, or Spring Tool Suite (I will be using IntelliJ IDEA).
Getting Started
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.
GroupId
is your unique organizational name (in most cases, people use their company’s reverse domain name such as “com.mycompany”), ArtifactId
is the unique name of the project, and Version
is the version number of the project.
After completing the setup, you should have an empty project with a Maven config file pom.xml
.
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 pom.xml
file
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
- The
<parent>
tag tells Maven to inherit the properties ofspring-boot-starter-parent
, such as port number, configs, etc. All these are provided by Spring. - The
<dependencies>
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. - The
<build>
contains build plugins, such asspring-maven-plugin
. - To get in-depth knowledge on Spring Boot Training
Update the Maven repository to download the dependencies.
We have now finished the configurations. Let’s start 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 MainApplicationClass.java
.
package me.salisuwy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MainApplicationClass { public static void main(String[] args) { SpringApplication.run(MainApplicationClass.class, args); } }
Make sure your class is inside a package, or it might not run. Also, take note of my package name (me.salisuwy
) which might be different from yours.
Add the @SpringBootApplication
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
localhost:8080
showing an error? Well, that is because we don’t 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.
Create BlogController.java
.
package me.salisuwy; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class BlogController { @RequestMapping("/") public String index() { return "Congratulations from BlogController.java"; } }
@RestController
annotation tells Spring that this class is a controller.@RequestMapping(“/”)
annotation means that any request (GET
,POST
,PUT
, etc.) to the rootURL(/)
will be handled by theindex()
method. The response is of typeString
.
Other variants of the @RequestMapping
annotation are @GetMapping
, @PostMapping
, etc. for handling GET
and POST
requests, respectively.
Take your career to new heights of success By enrolling for live free demo on Spring Boot Certification