Getting Started | Building an Application with Spring Boot

Getting Started | Building an Application with Spring Boot

Writing my first REST API using Spring Boot.

Shubham Deshmukh's photo
Shubham Deshmukh
·Jul 6, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Hello Friends, Today we will understand and write our Spring Boot REST API from scratch. We will go through the pre-requisites and later will write the code.

Tip : If you are new to java, Please visit this link to have better understanding of Java.


The requirement for running our first Spring Boot Application.

For executing any Spring Boot program, the following steps must be followed.


Creating Spring Boot Project

Navigate to start.spring.io in your favorite browser. This is spring intializr which helps us to create a skeleton for our application.

  • Add project metadata as you like or you can refer to the screenshot below.
  • Choose either Maven and packaging as jar.
  • Click Dependencies and select Spring Web.
  • Click Generate and a Zip will be downloaded. This will contain all the required files for your project like the main class and pom.xml

Screenshot 2022-07-03 at 2.11.14 PM.png

  • unZip the Package and Import the project into IDE.

Screenshot 2022-07-03 at 2.14.02 PM.png

  • Once you import give some time to maven to import the required libraries. The project will look like the below once all dependencies are imported.

Screenshot 2022-07-03 at 2.16.35 PM.png

  • Now create a package with the name Controller.

Screenshot 2022-07-03 at 2.18.09 PM.png

  • Now that the package is ready we will add a controller to our application. create a new class with the name HelloController.java.

Screenshot 2022-07-03 at 2.19.20 PM.png

  • Add the below code to the class.
package com.example.FirstAPI.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
 public String index() {
  return "Greetings from Spring Boot!";
 }
}
  • Run the application.

Screenshot 2022-07-03 at 2.23.04 PM.png

Suppose you are not getting the option to run the application as a spring boot application. Run it as a Java Application and give FirstApiApplication.java as an entry point.

  • Output Logs

Screenshot 2022-07-03 at 2.25.37 PM.png

Our Application is UP.


Running the API.

Go to the browser and hit localhost:8080

Screenshot 2022-07-03 at 2.28.11 PM.png


Thanks for reading. I hope this story was helpful.

If you are interested, check out my other articles.

you can also visit shubhamdeshmukh.com.

Did you find this article valuable?

Support Shubham Deshmukh by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this