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
Loading editor…

2)Use the following URL at Browser

Example03
Loading editor…

2)Use the following URL at Browser

Example04
Loading editor…

2)Use the following URL at Browser

Example05
Loading editor…

2)Use the following URL at Browser

Example06
Loading editor…

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
Loading editor…

2)Use the following URL at Browser

hellopage.jsp

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

Example08
Loading editor…

2)Use the following URL at Browser

wishpage.jsp:

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

Example09
Loading editor…

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
Loading editor…

2)Use the following URL at Browser

Example11
Loading editor…

HelloController.java

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

package com.durgasoft.controller;

HelloController.java

Example13
Loading editor…

HelloController.java

Example14
Loading editor…

HelloController.java

Example15
Loading editor…

HelloController.java

Example16
Loading editor…

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
Loading editor…

HelloappApplication.java

Example19
Loading editor…

HelloappApplication.java

Example20
Loading editor…

HelloappApplication.java

Example21
Loading editor…

HelloappApplication.java

Example22
Loading editor…

HelloappApplication.java

Example23
Loading editor…

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
Loading editor…

HelloappApplication.java

success.jsp

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

Example25
Loading editor…

HelloappApplication.java

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

11.<h2 align="center">

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

Example26
Loading editor…

HelloappApplication.java

failure.jsp

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

Example27
Loading editor…

HelloappApplication.java

LoginController.java

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

Example28
Loading editor…

HelloappApplication.java

Example29
Loading editor…

HelloappApplication.java

Example30
Loading editor…

HelloappApplication.java

Example31
Loading editor…

HelloappApplication.java

Example32
Loading editor…

HelloappApplication.java

Example33
Loading editor…

HelloappApplication.java

Example34
Loading editor…

HelloappApplication.java

Example35
Loading editor…

HelloappApplication.java

status = "failure";

}

return status;

}

LoginappApplication.java

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

package com.durgasoft;

Example36
Loading editor…

HelloappApplication.java

Example37
Loading editor…

HelloappApplication.java

Example38
Loading editor…

HelloappApplication.java

Example39
Loading editor…

HelloappApplication.java

Application.properties

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

server.port = 1234

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

spring.mvc.view.suffix=.jsp

pom.xml

-----------

Example40
Loading editor…

HelloappApplication.java

sd/maven-4.0.0.xsd">

Example41
Loading editor…

HelloappApplication.java

Example42
Loading editor…

HelloappApplication.java

Example43
Loading editor…

HelloappApplication.java

Example44
Loading editor…

HelloappApplication.java

Example45
Loading editor…

HelloappApplication.java

Example46
Loading editor…

HelloappApplication.java

Example47
Loading editor…

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
Loading editor…
📝 Key Takeaways
  • Key ideas of Spring Boot - REST Controllers explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.