Nearby lessons
6 of 10Spring Boot - REST Controllers
- 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">
2)Use the following URL at Browser
2)Use the following URL at Browser
2)Use the following URL at Browser
2)Use the following URL at Browser
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:
---
2)Use the following URL at Browser
hellopage.jsp
---------------
2)Use the following URL at Browser
wishpage.jsp:
------------------
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>
2)Use the following URL at Browser
HelloController.java
----------------------------
package com.durgasoft.controller;
HelloController.java
HelloController.java
HelloController.java
HelloController.java
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">
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
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:
------------------
HelloappApplication.java
success.jsp
---------------
HelloappApplication.java
10.<h1 style="color: red;" align="center">Login Success</h1>
11.<h2 align="center">
12.<a href="loginpage">|Login Page|</a>
HelloappApplication.java
failure.jsp
--------------
HelloappApplication.java
LoginController.java
---------------------------
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
status = "failure";
}
return status;
}
LoginappApplication.java
--------------------------
package com.durgasoft;
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
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">
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
HelloappApplication.java
First Execute LoginappApplication as standalone application then open browser and access
the application by using the following URL.
http://localhost:1234/loginpage.
- Key ideas of Spring Boot - REST Controllers explained simply
- Ready-to-use code examples
- Exam-style questions at the end