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




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.




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;

public class HomeController {
	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: