Spring Boot + Rest Controller Simple Demo

Spring boot is becoming familiar along with angular. spring boot can be used for standalone and batch projects. In Angular it is used for backend with rest controllers. So knowing how to use spring boot with rest controller will be very helpful for angular backend java developments.

Prerequisite:

Spring STS Tool

If you dont have you can download and install from,

 

Spring STS Download Link

 

 

What this program explains:

This below steps explains how to create a simple spring boot application in spring sts and how to create simple rest controller for the given roots like (/ or /login or /register) etc.

 

Remember if you are not using rest controller and want to use @Controller annotation then you need to have some view resolver like thymeleaf inorder to render the html output in the browsers.

 

When you are going to develop a backend application for front end frameworks like angular then knowing how to integrate spring boot with rest controller will help you a lot. Because from angular/front end frameworks all the crud operations will be executed through some rest or restless api’s only.

 

Video Tutorial:

 

Step 1: Create spring starter project

 

 

 

Step 2: Select Web alone from the available dependencies

 

 

Click finish to create the maven spring boot project.

 

 

Step 3: Create FrontPageRestController.java

 

 

SpringBootRestController/src/main/java

[java]

package in.javadomain;

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

@org.springframework.web.bind.annotation.RestController
public class FrontPageRestController {

@RequestMapping(“/”)
public String frontPage() {
return “welcome to front page”;
}

@RequestMapping(“/login”)
public String loginPage() {
return “welcome to login page”;
}

}

[/java]

 

 

 

Step 4: Run the Application as “Spring Boot App”

 

 

 

Step 5: Check the output in Browser

 

 

 

With /login root:

 

 

Note:

We have used Rest Controller so whatever we returned from the methods printed on the browser. If we would have used any view resolvers like thymeleaf, then we need to have the file in templates folder to print the html outputs.

 

Leave a Reply