0
0
Spring Bootframework~30 mins

@PathVariable for URL parameters in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Using @PathVariable for URL Parameters in Spring Boot
📖 Scenario: You are building a simple Spring Boot web service that returns greetings for users based on their names provided in the URL.
🎯 Goal: Create a Spring Boot controller that uses @PathVariable to extract a user's name from the URL and return a greeting message.
📋 What You'll Learn
Create a Spring Boot controller class named GreetingController.
Add a method that handles GET requests to /greet/{name}.
Use @PathVariable to capture the name parameter from the URL.
Return a greeting message that includes the captured name.
💡 Why This Matters
🌍 Real World
Web services often need to get data from the URL path to customize responses, like user profiles or product details.
💼 Career
Understanding @PathVariable is essential for backend developers working with Spring Boot to build REST APIs.
Progress0 / 4 steps
1
Create the GreetingController class
Create a public class called GreetingController annotated with @RestController.
Spring Boot
Need a hint?

Use @RestController to make the class a REST controller in Spring Boot.

2
Add a GET mapping method with @PathVariable
Inside GreetingController, add a public method called greetUser that handles GET requests to /greet/{name} using @GetMapping. The method should take a String name parameter annotated with @PathVariable.
Spring Boot
Need a hint?

Use @GetMapping("/greet/{name}") and @PathVariable String name in the method signature.

3
Implement the greeting message
In the greetUser method, return a greeting string that says "Hello, " followed by the name parameter.
Spring Boot
Need a hint?

Use string concatenation to combine "Hello, " and the name variable.

4
Add class-level request mapping
Add @RequestMapping("/api") annotation to the GreetingController class to prefix all routes with /api.
Spring Boot
Need a hint?

Use @RequestMapping("/api") above the class to add a common URL prefix.