How to create a Spring Boot REST API in 15 minutes

Hey guys,

Follow me on insta: https://www.instagram.com/global.software.developers/?hl=en

How to build a simple rest API using spring boot and java
Prerequisite for this tutorial is that you have java and eclipse installed on your system
Step 1
Open your eclipse
Right-click on the project explorer tab
And create a new project of type other
Step 2
A wizard will open
In the search section of the wizard, type maven
From the filtered options, select maven option and click on next
Step 3
Checkmark the box that says create a simple archetype maven project and click on next
Step 4
In group id
Artifact id
Name
And description
Type the name of your project
In my case the name of the project is helloworldproj
And click on finish
Step 5
Open google
Type in spring boot web starter dependency
Open mvnrepository link
Click on the latest release dependency and copy the maven dependency
As shown in the screenshot
Step 6
Open pom.xml of helloworldproj that you created
Before project closing XML tag, create a dependencies XML tag
Within dependencies, XML tag, paste the dependency that you copied from mvnrepository
Save the file
Select the project and refresh the project
On mac, 💻 the short key for a refresh is fn + opt + f5
On windows, I think it is alt + shift + f5
Refreshing the project will download all the dependency jars mentioned in the spring boot web started bundle
Step 7
Now right click on src/main/java folder
Click ok create a new package
And create two packages with names:-
One with the name “main”
And one with name “main.controllers”
Step 8
Your project’s folder structure will now look something like this pic
Step 9
In the main package, create an Application.class with a main method
Annotate class with @SpringBootApplication
And in the main method, write SpringApplication.run(Application.class, args);
When you run this main method as a java program, your web server will come up on your local system
Step 10
In the main.controllers package
Create a HelloWorldController.java file
Annotate it with @RestController
Provide an @RequestMapping and give it a value /
This is also known as the root controller
Now create an @GetMapping method in the file and simply return a “Hello World” string from it
That’s it, we’re done with the coding part
Step 11
Now go to Application.java file
Right-click on the main method and run it as a java program
This will start the embedded tomcat server and your first REST API will be deployed on your local system
Step 12
When you will run your spring boot app like a java program, the following logs will be generated in the console
By default your spring boot app with start at port 8080
Step 13
The final step is to check that your rest API is working as expected
For that, open any browser and type in localhost:8080
The browser will display Hello World text
This is how you create a hello world rest API!
More tutorials to come!
Keep following and share in your story for your friends to learn too !!
This marks the end of the spring boot basic rest api hello world tutorial !
If you have any doubts, leave a comment, or directly dm me !
I hope this tutorial was useful for you, if you did find it useful, consider sharing it in your story for your friends to see and learn too !

Best Regards,

Rakesh

Follow me on insta: https://www.instagram.com/global.software.developers/?hl=en

Software engineer @ DTDL