How to Create Simple REST APIs with Spring Boot
As a part of this article you will be building 2 simple REST apis Using Springboot. This article will give an idea on how to quickly get started with springboot.
So let’s get started
Pre-requisite
Ensure you have Maven Installed in your system before starting with this article.
You can Install Maven from https://maven.apache.org/
Also ensure Maven is set in the PATH so that mvn
comands work.
you can verify if maven is installed and can be accessed using the command
Also ensure JAVA_HOME is set.
Project Setup
The first step is to setup your project.
Setting up a Springboot project is pretty easy.
Go to https://start.spring.io/.
In the Site enter the Artifact name as simple-rest-apis and under dependencies add Web. You can enter any Other Artifact name as well.
Also in the top Ensure you are creating a Maven Project with Java using Springboot version 2.0.6 This is to ensure that the results in this article are reproducible. Once you are done with this article, You can experiment by choosing the other options
Once you enter the information, the screen should look like this
Click on Generate Project, this will download a zip file onto your computer. Extract this zip file. The extracted folder is your springboot project.
You can import the project into your preferred IDE. I used eclipse to try this out.
Project Structure Explained
pom.xml
This file has all the maven dependencies. The main dependency to note is the following
spring-boot-starter-web dependency ensures that the project can be used for web applications
Also one other important thing to notice in pom.xml is the following
spring-boot-starter-parent is made as the parent of this project. This ensures that any internal dependencies needed by springboot are automatically taken care off, and the developer need not worry about it.
SimpleRestApisApplication.Java
This file is named after the project name, followed by an Application.
This file is present inside src/main/java folder and inside com.example.simplerestapis package.
This file has the following piece of code
The main highlight here is the annotation @SpringBootApplication . This internally is a combination of the following 3 annotations
- @Configuration: Needed for Manual Spring configurations. Adding this annotation ensures that configuration can be done in a java class itself instead of using a separate xml file.
- @EnableAutoConfiguration: Spring needs a lot of configuration to be done. This annotation ensures that a lot of the configuration is done automatically.
- @ComponentScan: This tells Spring, where all to scan for components.
The Line SpringApplication.run(SimpleRestApisApplication.class, args);
bootstraps the application.
Application.properties
This file is present inside src/main/resources. The file can be used to list out the various properties to use while running the application. For example it can be used to mention on which port the application should run.
Create your first API
Our First API will be a simple API demonstrating GET request.
Create a package called com.example.simplerestapis.models. Inside this package create a file called as SampleResponse.java
Copy the following code into SampleResponse.java
SampleResponse.java is just a model class. It indicates the fields which are present in the response of your api.
Create a package called com.example.simplerestapis.controller. Inside this package create a file called as WebController.java
Copy the following code into WebController.java
In the Above Code the Annotation @RestController indicates that this class will have Rest End points. This Annotation basically tells that this class is a controller and the value returned from a function in this class will either be converted to JSON or XML. Default in JSON.
@RequestMapping maps the /sample endpoint to Sample Function
@RequestParam indicates that the endpoint /sample will have one Query parameter called name. The default value of name is “Robot”
The code inside the function is pretty straight forward. The response is being set based on name value.
Go to command prompt. Go into your project folder and run the following command to start the application
By Default the application runs on port 8080 in localhost.
In order to test api end points you can use Postman.
Thank You and Happy Coding…..😄😄😄