Nearby lessons

6 of 10

Spring Boot - REST Controllers

📌 What You Will Learn
  • Understand Spring Boot - REST Controllers
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Spring Boot - REST Controllers step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

2)Use the following URL at Browser

http://localhost:1234/hello

Postman:

--------

Postman is a web tool, it can be used to send different types of request like GET, POST,

PUT, OPTIONS,......, but, it required HttpServer environment.

Steps:

------

1.Download and install Postman tool.

2.Open Postman tool and send request to Spring boot Application which is running at

present.

1.Download and install Postman tool:

---------------------------------------------------

a)Open "https://www.getpostman.com/downloads/" link in brower Addressbar.

b)Click on Download button and select Windows 64 bit

With the above Postman tool will download with the setup file "Postman-win64-7.8.0-

Setup.exe.

c)Execute Postman setup file by double clicking on "Postman-win64-7.8.0-Setup.exe".

d)Provide signin details or click on Open with google Account.

With the above Postman tool will open.

2.Send request to Spring boot Application which is running at present:

------------------------------------------------------------------------------------------------

  • Enter Application URL
  • Select the request type like GET or POST,...
  • Click on Send button and check the response in Body part.

EX:

---

application.properties

----------------------

server.port=1234

HelloappApplication.java

-------------------------

package com.durgasoft;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class HelloappApplication {

public static void main(String[] args) {

SpringApplication.run(HelloappApplication.class, args);

}

}

HelloController.java

---------------------

package com.durgasoft.controller;

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

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

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

@RestController

public class HelloController {

@RequestMapping(value="/hello",method = RequestMethod.GET )

public String sayHello() {

return "Hello User!";

}

@RequestMapping(value="/hello",method = RequestMethod.POST )

public String sayWelcome() {

return "Welcome User!";

}

}

pom.xml:

-----------

2)Use the following URL at Browser

/XMLSchema-instance"

  • xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/x

sd/maven-4.0.0.xsd">

Example02
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001
4

2)Use the following URL at Browser

Example03
JCode Cell
1 
2<modelVersion>4.0.0</modelVersion>
3<parent>
4<groupId>org.springframework.boot</groupId>
5<artifactId>spring-boot-starter-parent</artifactId>
6<version>2.1.9.RELEASE</version>
7<relativePath/> <!-- lookup parent from repository -->
8</parent>
9<groupId>com.durgasoft</groupId>
10<artifactId>helloapp</artifactId>
11<version>0.0.1-SNAPSHOT</version>
12<name>helloapp</name>
13<description>helloapp for Spring Boot</description>
14

2)Use the following URL at Browser

Example04
JCode Cell
1 
2<properties>
3<java.version>1.8</java.version>
4</properties>
5

2)Use the following URL at Browser

Example05
JCode Cell
1 
2<dependencies>
3<dependency>
4<groupId>org.springframework.boot</groupId>
5<artifactId>spring-boot-starter-web</artifactId>
6</dependency>
7

2)Use the following URL at Browser

Example06
JCode Cell
1 
2<dependency>
3<groupId>org.springframework.boot</groupId>
4<artifactId>spring-boot-starter-test</artifactId>
5<scope>test</scope>
6</dependency>
7</dependencies>
8

2)Use the following URL at Browser

In Postman.

If we send GET request with the url "http://localhost:1234/hello" then we will get "Hello

User!" response.

If we send POST request with the url "http://localhost:1234/hello" then we will get

"Welcome User!" response.

How to use User forms in Spring boot Application:

--------------------------------------------------------------------

  • Prepare Spring starter project in STS.
  • Prepare "webapp/WEB-INF" folders under "src/main" folders to provide JSP and html

pages.

  • Add "tomcat-jasper" dependency in pom.xml inorder to support for JSP.
  • Prepare HTML, JSP pages under "WEB-INF" folder.
  • Prepare Controller classes as per Spring WEB MVC and as per the requirement.
  • Provide server.port=1234, spring.mvc.view.prefix=/WEB-INF/, spring.mvc.view.suffix=.jsp

key value pairs in application.properties file.

  • Access SpringBoot application as standalone application.
  • Open browser and send request.

EX:

---

Example07
JCode Cell
1 
2<build>
3<plugins>
4<plugin>
5<groupId>org.springframework.boot</groupId>
6<artifactId>spring-boot-maven-plugin</artifactId>
7</plugin>
8</plugins>
9</build>
10</project>
11

2)Use the following URL at Browser

hellopage.jsp

---------------

Example08
JCode Cell
1 
2helloapp
3|---/src/main/java
4| |---com.durgasoft
5| |-----HelloappApplication.java
6|---com.durgasoft.controller
7| |HelloController.java
8|---/src/main/resources
9| |---static
10| |---templates
11| |---application.properties
12|---/src/test/java
13|---src
14| |---main
15| |----webapp
16| |----WEB-INF
17| |---hellopage.jsp
18| |---wishpage.jsp
19|---/src/test
20|---target
21|---HELP.md
22|---mvnw
23|---mvnw.cmd
24|---pom.xml
25

2)Use the following URL at Browser

wishpage.jsp:

------------------

Example09
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html>
5<html>
6<head>
7<meta charset="ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: red;" align="center">Durga Software Solutions</h2>
12<h3 style="color: blue;" align="center">User Hello Form</h3>
13<form method="post" action="wish">
14<table align="center">
15<tr>
16<td>User Name</td>
17<td><input type="text" name="username"></td>
18</tr>
19<tr>
20<td><input type="submit" value="SayHello"/></td>
21</tr>
22</table>
23</form>
24</body>
25</html>
26

2)Use the following URL at Browser

10.<h1 style="color: red;" align="center">Hello ${username}!</h1>

11.<h2 align="center">

12.<a href="hellopage">|User Hello Page|</a>

Example10
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html>
5<html>
6<head>
7<meta charset="ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11

2)Use the following URL at Browser

Example11
JCode Cell
1 
2</h2>
3</body>
4</html>
5

HelloController.java

----------------------------

package com.durgasoft.controller;

HelloController.java

Example13
JCode Cell
1 
2import org.springframework.stereotype.Controller;
3import org.springframework.ui.ModelMap;
4import org.springframework.web.bind.annotation.RequestMapping;
5import org.springframework.web.bind.annotation.RequestMethod;
6import org.springframework.web.bind.annotation.RequestParam;
7

HelloController.java

Example14
JCode Cell
1 
2@Controller
3public class HelloController {
4

HelloController.java

Example15
JCode Cell
1 
2@RequestMapping(value="/hellopage",method = RequestMethod.GET )
3public String sayHello() {
4return "hellopage";
5}
6

HelloController.java

Example16
JCode Cell
1 
2@RequestMapping(value="/wish",method = RequestMethod.POST )
3public String wish(@RequestParam String username, ModelMap map) {
4map.addAttribute("username", username);
5return "wishpage";
6}
7}
8

HelloappApplication.java

--------------------------------

package com.durgasoft;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class HelloappApplication {

public static void main(String[] args) {

SpringApplication.run(HelloappApplication.class, args);

}

}

Application.properties:

------------------------------

server.port=1234

spring.mvc.view.prefix = /WEB-INF/

spring.mvc.view.suffix = .jsp

pom.xml:

-----------

HelloappApplication.java

sd/maven-4.0.0.xsd">

Example18
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<project xmlns="http://maven.apache.org/POM/4.0.0"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/x
6

HelloappApplication.java

Example19
JCode Cell
1 
2<modelVersion>4.0.0</modelVersion>
3<parent>
4<groupId>org.springframework.boot</groupId>
5<artifactId>spring-boot-starter-parent</artifactId>
6<version>2.1.9.RELEASE</version>
7<relativePath /> <!-- lookup parent from repository -->
8</parent>
9<groupId>com.durgasoft</groupId>
10<artifactId>helloapp</artifactId>
11<version>0.0.1-SNAPSHOT</version>
12<name>helloapp</name>
13<description>helloapp for Spring Boot</description>
14

HelloappApplication.java

Example20
JCode Cell
1 
2<properties>
3<java.version>1.8</java.version>
4</properties>
5

HelloappApplication.java

Example21
JCode Cell
1 
2<dependencies>
3<dependency>
4<groupId>org.springframework.boot</groupId>
5<artifactId>spring-boot-starter-web</artifactId>
6</dependency>
7

HelloappApplication.java

Example22
JCode Cell
1 
2<dependency>
3<groupId>org.apache.tomcat</groupId>
4<artifactId>tomcat-jasper</artifactId>
5<version>9.0.26</version>
6</dependency>
7

HelloappApplication.java

Example23
JCode Cell
1 
2<dependency>
3<groupId>org.springframework.boot</groupId>
4<artifactId>spring-boot-starter-test</artifactId>
5<scope>test</scope>
6</dependency>
7</dependencies>
8

HelloappApplication.java

EX:

---

loginapp

|---/src/main/java

| |---com.durgasoft

| |-----LoginappApplication.java

|---com.durgasoft.controller

| |LoginController.java

|---/src/main/resources

| |---static

| |---templates

| |---application.properties

|---/src/test/java

|---src

| |---main

| |----webapp

| |----WEB-INF

| |---failure.jsp

| |---loginform.jsp

| |---success.jsp

|---/src/test

|---target

|---HELP.md

|---mvnw

|---mvnw.cmd

|---pom.xml

loginform.jsp:

------------------

Example24
JCode Cell
1 
2<build>
3<plugins>
4<plugin>
5<groupId>org.springframework.boot</groupId>
6<artifactId>spring-boot-maven-plugin</artifactId>
7</plugin>
8</plugins>
9</build>
10</project>
11

HelloappApplication.java

success.jsp

---------------

Example25
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html>
5<html>
6<head>
7<meta charset="ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h2 style="color: red;" align="center">Durga Software Solutions</h2>
12<h3 style="color: blue;" align="center">User Login Form</h3>
13<form method="post" action="/login">
14<table align="center">
15<tr>
16<td>User Name</td>
17<td><input type="text" name="uname"></td>
18</tr>
19<tr>
20<td>Password</td>
21<td><input type="password" name="upwd"></td>
22</tr>
23<tr>
24<td><input type="submit" value="Login"/></td>
25</tr>
26</table>
27</form>
28</body>
29</html>
30

HelloappApplication.java

10.<h1 style="color: red;" align="center">Login Success</h1>

11.<h2 align="center">

12.<a href="loginpage">|Login Page|</a>

Example26
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html>
5<html>
6<head>
7<meta charset="ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11

HelloappApplication.java

failure.jsp

--------------

Example27
JCode Cell
1 
2</h2>
3</body>
4</html>
5

HelloappApplication.java

LoginController.java

---------------------------

Example28
JCode Cell
1 
2<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
3pageEncoding="ISO-8859-1"%>
4<!DOCTYPE html>
5<html>
6<head>
7<meta charset="ISO-8859-1">
8<title>Insert title here</title>
9</head>
10<body>
11<h1 style="color: red;" align="center">Login Success</h1>
12<h2 align="center">
13<a href="loginpage">|Login Page|</a>
14</h2>
15</body>
16</html>
17

HelloappApplication.java

Example29
JCode Cell
1 
2package com.durgasoft.controller;
3

HelloappApplication.java

Example30
JCode Cell
1 
2import org.springframework.stereotype.Controller;
3import org.springframework.web.bind.annotation.RequestMapping;
4import org.springframework.web.bind.annotation.RequestMethod;
5import org.springframework.web.bind.annotation.RequestParam;
6

HelloappApplication.java

Example31
JCode Cell
1 
2@Controller
3public class LoginController {
4

HelloappApplication.java

Example32
JCode Cell
1 
2@RequestMapping(value="/loginpage", method = RequestMethod.GET)
3public String showLoginPage() {
4return "loginform";
5

HelloappApplication.java

Example33
JCode Cell
1 
2}
3

HelloappApplication.java

Example34
JCode Cell
1 
2@RequestMapping(value="/login", method = RequestMethod.POST)
3public String login(@RequestParam String uname, @RequestParam String upwd) {
4

HelloappApplication.java

Example35
JCode Cell
1 
2String status = "";
3if(uname.equals("durga") && upwd.equals("durga")) {
4

HelloappApplication.java

status = "failure";

}

return status;

}

LoginappApplication.java

--------------------------

package com.durgasoft;

Example36
JCode Cell
1 
2status = "success";
3}else {
4}
5

HelloappApplication.java

Example37
JCode Cell
1 
2import org.springframework.boot.SpringApplication;
3import org.springframework.boot.autoconfigure.SpringBootApplication;
4

HelloappApplication.java

Example38
JCode Cell
1 
2@SpringBootApplication
3public class LoginappApplication {
4

HelloappApplication.java

Example39
JCode Cell
1 
2public static void main(String[] args) {
3SpringApplication.run(LoginappApplication.class, args);
4}
5

HelloappApplication.java

Application.properties

------------------------

server.port = 1234

spring.mvc.view.prefix=/WEB-INF/

spring.mvc.view.suffix=.jsp

pom.xml

-----------

Example40
JCode Cell
1 
2}
3

HelloappApplication.java

sd/maven-4.0.0.xsd">

Example41
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<project xmlns="http://maven.apache.org/POM/4.0.0"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/x
6

HelloappApplication.java

Example42
JCode Cell
1 
2<modelVersion>4.0.0</modelVersion>
3<parent>
4<groupId>org.springframework.boot</groupId>
5<artifactId>spring-boot-starter-parent</artifactId>
6<version>2.1.9.RELEASE</version>
7<relativePath /> <!-- lookup parent from repository -->
8</parent>
9<groupId>com.durgasoft</groupId>
10<artifactId>loginapp</artifactId>
11<version>0.0.1-SNAPSHOT</version>
12<name>loginapp</name>
13<description>Spring boot Login app</description>
14

HelloappApplication.java

Example43
JCode Cell
1 
2<properties>
3<java.version>1.8</java.version>
4</properties>
5

HelloappApplication.java

Example44
JCode Cell
1 
2<dependencies>
3<dependency>
4<groupId>org.springframework.boot</groupId>
5<artifactId>spring-boot-starter-web</artifactId>
6</dependency>
7

HelloappApplication.java

Example45
JCode Cell
1 
2<dependency>
3<groupId>org.apache.tomcat</groupId>
4<artifactId>tomcat-jasper</artifactId>
5<version>9.0.26</version>
6</dependency>
7

HelloappApplication.java

Example46
JCode Cell
1 
2<dependency>
3<groupId>org.springframework.boot</groupId>
4<artifactId>spring-boot-starter-test</artifactId>
5<scope>test</scope>
6</dependency>
7</dependencies>
8

HelloappApplication.java

Example47
JCode Cell
1 
2<build>
3<plugins>
4<plugin>
5<groupId>org.springframework.boot</groupId>
6<artifactId>spring-boot-maven-plugin</artifactId>
7</plugin>
8</plugins>
9</build>
10

HelloappApplication.java

First Execute LoginappApplication as standalone application then open browser and access

the application by using the following URL.

http://localhost:1234/loginpage.

Example48
JCode Cell
1 
2</project>
3
📝 Key Takeaways
  • Key ideas of Spring Boot - REST Controllers explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end