0
0
Spring Bootframework~30 mins

@RequestMapping for base paths in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Using @RequestMapping for Base Paths in Spring Boot
📖 Scenario: You are building a simple Spring Boot web application that manages greetings. You want to organize your controller so that all greeting-related URLs start with the same base path.
🎯 Goal: Create a Spring Boot controller class with a base path using @RequestMapping at the class level, and add methods to handle specific greeting requests under that base path.
📋 What You'll Learn
Create a controller class named GreetingController
Use @RequestMapping at the class level with the base path /greetings
Add a method hello() mapped to /hello that returns the string Hello, World!
Add a method goodbye() mapped to /goodbye that returns the string Goodbye, World!
💡 Why This Matters
🌍 Real World
Organizing controller URLs with base paths helps keep your web application routes clean and easy to manage.
💼 Career
Understanding how to use @RequestMapping for base paths is essential for building REST APIs and web applications in Spring Boot, a common skill for Java backend developers.
Progress0 / 4 steps
1
Create the GreetingController class
Create a public class named GreetingController annotated with @RestController.
Spring Boot
Need a hint?

Use @RestController to make the class a REST controller.

2
Add @RequestMapping base path to the class
Add @RequestMapping("/greetings") annotation to the GreetingController class to set the base path for all methods.
Spring Boot
Need a hint?

Place @RequestMapping("/greetings") above the class declaration.

3
Add hello() method mapped to /hello
Inside GreetingController, add a public method named hello annotated with @RequestMapping("/hello") that returns the string Hello, World!.
Spring Boot
Need a hint?

Use @RequestMapping("/hello") above the method and return the exact string.

4
Add goodbye() method mapped to /goodbye
Add a public method named goodbye annotated with @RequestMapping("/goodbye") that returns the string Goodbye, World! inside GreetingController.
Spring Boot
Need a hint?

Use @RequestMapping("/goodbye") above the method and return the exact string.