January 13, 2021

Spring Boot – Add REST Endpoint

In this tutorial, we are going to be adding a REST Endpoint to our Spring application.

Now, what is a REST Endpoint?

A REST endpoint makes it possible for a web application to respond to HTTP REST requests. Therefore, if a user goes to the browser and enters a url, we want to tell spring to run a specific method and send the return value to the user.

 

These are the Steps to Follow

There are six simple steps to follows. They are listed and explained below.

 

Step 1: Add the starter-web dependency

This dependency allows your application to function as a web application. If you add this dependency, then when you run your application, a tomcat server will be started and your application is automatically deployed.

To add this starter web dependency, copy and paste this code below in your pom.xml file

 

<dependency>
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.1.2.RELEASE</version>
</dependency>

 

Step 2: Add the spring webmvc dependency

This dependency is used to make your application follow the mvc design pattern. To add the spring webmvc dependency, copy and paste the code below into your pom.xml.

 

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>

 

Note: You can find any dependency you want in Maven Repository

 

Step 3: Create a class (simple .java file)

You can create this class in the same package as the base class. For me, this i called the class HomeController.

 

Step 4: Annotate the class with @RestController

You use the @RestController annotation to tell Spring that this class contains methods that would respond to incoming REST HTTP request.

 

Step 5: Write the method you want to run

We write a simple method to return a string. The default request method is GET. The whole content of the class file is given below:

 

package com.kindsonthegenius.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {
	
	@RequestMapping(value="/")
	public static String Welcome() {
	    return "Welcome to Spring Boot \n" +
		"Remember to subscribe and leave a comment";
	}

}

 

Step 6: Annotate the method with @RequestMapping

The @RequestMapping  maps a particular url pattern to a method. In our example, the url pattern is “/” and it maps to the method we just wrote. So when a user makes http request to the “/” url, it executes the method we wrote.

You an watch the complete video explanation of this procedure below:

0 0 vote
Article Rating
Subscribe
Notify of
guest
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anselm Paulinus
Anselm Paulinus
1 year ago

Whitelabel Error Page

I found your lectures only yesterday and I followed your tutorial up to lesson 10. where you ran the test localhost:8080/welcome on the browser, but mine returned the error below,even though my tomcat server started okay. I have googled the error, and no solution has worked for me so far. Don’t know if any one else reported getting this problem.

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Apr 08 21:42:28 MDT 2019
There was an unexpected error (type=Not Found, status=404).
No message available

ana banana
ana banana
9 months ago

Everything is fine, I just had to change the port because apparently there is a java process in port 8080

stevesKamdem
stevesKamdem
8 months ago

hello
I am a beginner on spring, I saw your training like it is cool

okpara obinna
okpara obinna
7 months ago

I am actually enjoying every bit of this learning process i actual know spring to this extent but got a better understanding now, and this is a sure bet for me to understand this well.

samia
samia
6 months ago

hello i followed the steps you did but i have this error “The Tomcat connector configured to listen on port 8081 failed to start. The port may already be in use or the connector may be misconfigured.” i changed the number of the port many times but still the error shows up

Bassel
Bassel
2 months ago

Hello kindsonthegenius I will appreciate your efforts doing this training

I need to talk you for an important issue as fast as possible ,
can I pleas?