📖 Scenario: You are building a simple web application using Spring Boot. To make your app work correctly, you need to understand how a user's request travels through your app from the moment it arrives until the response is sent back.
🎯 Goal: Build a basic Spring Boot controller that handles a web request and returns a greeting message. This will help you see the request flow in action.
📋 What You'll Learn
Create a Spring Boot controller class named GreetingController
Add a method that handles GET requests at the path /greet
Return a simple greeting message as a string
Configure the application to run as a Spring Boot app
💡 Why This Matters
🌍 Real World
Understanding request flow helps developers build web apps that respond correctly to user actions, like loading pages or submitting forms.
💼 Career
Most Java backend developer roles require knowledge of Spring Boot request handling to create APIs and web services.
Progress0 / 4 steps
1
Create the Controller Class
Create a public class called GreetingController and annotate it with @RestController.
Spring Boot
Hint
The @RestController annotation tells Spring Boot this class will handle web requests and return data directly.
2
Add a GET Request Handler Method
Inside GreetingController, add a public method called greet that returns a String. Annotate it with @GetMapping("/greet").
Spring Boot
Hint
The @GetMapping annotation maps HTTP GET requests to this method.
3
Create the Main Application Class
Create a class called Application with a main method. Annotate the class with @SpringBootApplication and inside main, call SpringApplication.run(Application.class, args);.
Spring Boot
Hint
The @SpringBootApplication annotation marks the main class for Spring Boot to start the app.
4
Run and Test the Application
Add the @SpringBootApplication import and ensure the application runs. The GreetingController should handle GET requests to /greet and return the greeting message.
Spring Boot
Hint
Make sure you have all necessary imports so the app compiles and runs.
Practice
(1/5)
1. Why is it important to understand the request flow in a Spring Boot application?
easy
A. It reduces the size of the application binary.
B. It allows you to write SQL queries faster.
C. It automatically improves the app's UI design.
D. It helps in debugging and adding new features effectively.
Solution
Step 1: Understand the role of request flow
The request flow shows how data moves through the app, which helps in tracking issues and adding features.
Step 2: Identify benefits related to debugging and features
Knowing the flow helps developers find bugs and extend functionality easily.
Final Answer:
It helps in debugging and adding new features effectively. -> Option D
Quick Check:
Request flow aids debugging and features = A [OK]
Hint: Request flow helps fix bugs and add features fast [OK]
Common Mistakes:
Thinking request flow changes UI design
Confusing request flow with database queries
Assuming it reduces app size
2. Which component in Spring Boot is responsible for receiving all HTTP requests first?
easy
A. Controller
B. DispatcherServlet
C. Service
D. Repository
Solution
Step 1: Identify the entry point for HTTP requests
In Spring Boot, DispatcherServlet is the front controller that receives all HTTP requests first.
Step 2: Differentiate from other components
Controllers handle requests after DispatcherServlet routes them; Services and Repositories handle business and data logic.
Final Answer:
DispatcherServlet -> Option B
Quick Check:
DispatcherServlet receives requests first = A [OK]
Hint: DispatcherServlet is the first to get HTTP requests [OK]
Common Mistakes:
Choosing Controller as the first receiver
Confusing Service or Repository with request entry
Thinking DispatcherServlet handles business logic
3. Given this Spring Boot controller method, what will be the response when a GET request is made to /hello?
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring!";
}
medium
A. The plain text 'Hello, Spring!' as the HTTP response body
B. A JSON object with message 'Hello, Spring!'
C. A redirect to another URL
D. An error because no ResponseEntity is returned
Solution
Step 1: Analyze the controller method return type
The method returns a String, which by default is sent as plain text in the HTTP response body.
Step 2: Understand Spring Boot default behavior for String returns
Without annotations like @ResponseBody missing, Spring Boot treats String return as response content, not a view name.
Final Answer:
The plain text 'Hello, Spring!' as the HTTP response body -> Option A
Quick Check:
String return sends plain text response = B [OK]
Hint: String return sends plain text unless annotated otherwise [OK]
Common Mistakes:
Assuming it returns JSON automatically
Expecting a redirect without code
Thinking ResponseEntity is mandatory
4. What is wrong with this Spring Boot controller method that causes it to not handle requests properly?
@GetMapping("/data")
public void getData() {
System.out.println("Data requested");
}
medium
A. System.out.println cannot be used inside controller methods.
B. The @GetMapping annotation is missing parentheses.
C. The method returns void, so no response is sent to the client.
D. The method should be private, not public.
Solution
Step 1: Check the method return type and response behavior
The method returns void and does not write to the response, so the client gets no content.
Step 2: Understand HTTP response requirements
Controllers must return a response body or view; void means no response, causing client issues.
Final Answer:
The method returns void, so no response is sent to the client. -> Option C
Quick Check:
Void return means no client response = D [OK]
Hint: Controller methods must return response, not void [OK]
Common Mistakes:
Thinking @GetMapping needs no parentheses
Believing method visibility causes error
Assuming System.out.println breaks controller
5. In a Spring Boot app, why is understanding the request flow crucial when adding security features like authentication filters?
hard
A. Because filters must be placed correctly in the flow to block unauthorized requests before reaching controllers.
B. Because security features automatically work without knowing the flow.
C. Because request flow only affects UI rendering, not security.
D. Because authentication filters replace DispatcherServlet.
Solution
Step 1: Understand the role of filters in request flow
Filters intercept requests early to check authentication before controllers handle them.
Step 2: Recognize why placement in flow matters for security
Placing filters correctly ensures unauthorized requests are blocked promptly, protecting the app.
Final Answer:
Because filters must be placed correctly in the flow to block unauthorized requests before reaching controllers. -> Option A
Quick Check:
Filters block unauthorized requests early = C [OK]
Hint: Security filters must run before controllers to block bad requests [OK]
Common Mistakes:
Assuming security works without flow knowledge
Thinking request flow only affects UI
Believing filters replace DispatcherServlet
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.