Bird
Raised Fist0
Spring Bootframework~20 mins

@PathVariable for URL parameters in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PathVariable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot controller method?
Consider this Spring Boot controller method:
@GetMapping("/users/{id}")
public String getUser(@PathVariable String id) {
    return "User ID: " + id;
}

What will be the response body when a client requests /users/42?
Spring Boot
@GetMapping("/users/{id}")
public String getUser(@PathVariable String id) {
    return "User ID: " + id;
}
AUser ID: 42
BUser ID: {id}
CUser ID: null
D404 Not Found error
Attempts:
2 left
💡 Hint
The @PathVariable annotation binds the URL part to the method parameter.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses @PathVariable with a different parameter name?
Given the URL pattern /items/{itemId}, which method signature correctly binds the path variable to a method parameter named id?
A
@GetMapping("/items/{itemId}")
public String getItem(@PathVariable("id") String id) { return id; }
B
@GetMapping("/items/{itemId}")
public String getItem(@PathVariable String itemId) { return id; }
C
@GetMapping("/items/{id}")
public String getItem(@PathVariable("itemId") String id) { return id; }
D
@GetMapping("/items/{itemId}")
public String getItem(@PathVariable("itemId") String id) { return id; }
Attempts:
2 left
💡 Hint
Use @PathVariable with the exact name of the path segment if the parameter name differs.
🔧 Debug
advanced
2:00remaining
What error occurs with this controller method?
Examine this Spring Boot controller method:
@GetMapping("/orders/{orderId}")
public String getOrder(@PathVariable int id) {
    return "Order: " + id;
}

What happens when a client requests /orders/123?
Spring Boot
@GetMapping("/orders/{orderId}")
public String getOrder(@PathVariable int id) {
    return "Order: " + id;
}
AHTTP 400 Bad Request due to type mismatch
BHTTP 400 Bad Request due to missing path variable binding
CHTTP 404 Not Found error
DReturns 'Order: 123' successfully
Attempts:
2 left
💡 Hint
Check if the method parameter name matches the path variable name or if @PathVariable specifies the name.
❓ state_output
advanced
2:00remaining
What is the output of this controller with multiple @PathVariable parameters?
Given this controller method:
@GetMapping("/books/{genre}/{id}")
public String getBook(@PathVariable String genre, @PathVariable int id) {
    return genre + "-" + id;
}

What is the response when the URL /books/scifi/7 is requested?
Spring Boot
@GetMapping("/books/{genre}/{id}")
public String getBook(@PathVariable String genre, @PathVariable int id) {
    return genre + "-" + id;
}
Abooks-scifi-7
Bscifi-null
Cscifi-7
D7-scifi
Attempts:
2 left
💡 Hint
Each @PathVariable binds to the matching segment in the URL path.
🧠 Conceptual
expert
2:00remaining
Which statement about @PathVariable is true?
Choose the correct statement about the use of @PathVariable in Spring Boot controllers.
A@PathVariable can bind multiple path segments separated by slashes into a single parameter using regex.
B@PathVariable requires the method parameter name to always match the path variable name in the URL.
C@PathVariable cannot be used with optional URL parameters.
D@PathVariable automatically converts path variables to any Java type without errors.
Attempts:
2 left
💡 Hint
Think about advanced usage of @PathVariable with patterns.

Practice

(1/5)
1. What is the main purpose of the @PathVariable annotation in Spring Boot?
easy
A. To bind form data to a Java object
B. To handle exceptions in controller methods
C. To define a fixed URL mapping for a controller method
D. To extract values from the URL path and pass them as method parameters

Solution

  1. Step 1: Understand the role of @PathVariable

    @PathVariable is used to get dynamic parts of the URL and pass them to method parameters.
  2. Step 2: Compare with other annotations

    Other options like binding form data or exception handling are done by different annotations, not @PathVariable.
  3. Final Answer:

    To extract values from the URL path and pass them as method parameters -> Option D
  4. Quick Check:

    @PathVariable = extract URL values [OK]
Hint: Remember: @PathVariable grabs values directly from URL path [OK]
Common Mistakes:
  • Confusing @PathVariable with @RequestParam
  • Thinking it binds form data
  • Assuming it handles exceptions
2. Which of the following is the correct syntax to use @PathVariable in a Spring Boot controller method to get an id from the URL /user/{id}?
easy
A. @GetMapping("/user/{id}") public String getUser(@PathVariable int id) { ... }
B. @GetMapping("/user") public String getUser(@PathVariable int id) { ... }
C. @GetMapping("/user/{id}") public String getUser(@RequestParam int id) { ... }
D. @GetMapping("/user/{id}") public String getUser(@PathVariable String name) { ... }

Solution

  1. Step 1: Match URL pattern with method parameter

    The URL has {id}, so the method must have @PathVariable int id to match the placeholder.
  2. Step 2: Check annotation and parameter type

    @GetMapping("/user/{id}") public String getUser(@PathVariable int id) { ... } correctly uses @PathVariable with int id and matches the URL pattern.
  3. Final Answer:

    @GetMapping("/user/{id}") public String getUser(@PathVariable int id) { ... } -> Option A
  4. Quick Check:

    URL {id} matches @PathVariable int id [OK]
Hint: URL placeholder and @PathVariable name must match [OK]
Common Mistakes:
  • Missing {id} in URL path
  • Using @RequestParam instead of @PathVariable
  • Mismatching parameter name and URL placeholder
3. Given the following Spring Boot controller method:
@GetMapping("/product/{category}/{id}")
public String getProduct(@PathVariable String category, @PathVariable int id) {
    return category + "-" + id;
}

What will be the output when a client requests /product/electronics/42?
medium
A. electronics-42
B. 42-electronics
C. category-id
D. electronics42

Solution

  1. Step 1: Identify parameter values from URL

    URL /product/electronics/42 means category = "electronics", id = 42.
  2. Step 2: Understand return statement

    The method returns category + "-" + id, so it returns "electronics-42".
  3. Final Answer:

    electronics-42 -> Option A
  4. Quick Check:

    Return = category + "-" + id = electronics-42 [OK]
Hint: Match URL parts to parameters in order [OK]
Common Mistakes:
  • Swapping parameter order
  • Ignoring the dash '-' in return
  • Confusing @PathVariable with @RequestParam
4. Consider this Spring Boot controller method:
@GetMapping("/order/{orderId}")
public String getOrder(@PathVariable int id) {
    return "Order: " + id;
}

What will happen when a client requests /order/100?
medium
A. Returns "Order: 0"
B. Returns "Order: 100"
C. Throws an error because parameter name does not match path variable
D. Returns null

Solution

  1. Step 1: Check URL placeholder and method parameter names

    URL has {orderId}, but method parameter is named id without specifying name in @PathVariable.
  2. Step 2: Understand default matching behavior

    Without specifying the name in @PathVariable, Spring tries to match parameter name to path variable name. They differ, causing an error.
  3. Final Answer:

    Throws an error because parameter name does not match path variable -> Option C
  4. Quick Check:

    Parameter name must match path variable or specify name [OK]
Hint: Parameter name must match path variable or use @PathVariable("name") [OK]
Common Mistakes:
  • Assuming names can differ without annotation
  • Ignoring the need to specify variable name in @PathVariable
  • Expecting default value assignment
5. You want to create a Spring Boot controller method that handles URLs like /blog/{year}/{month}/{slug} where year and month are integers and slug is a string. Which of the following method signatures correctly uses @PathVariable to extract these values?
hard
A. @GetMapping("/blog/{year}/{month}/{slug}") public String getPost(@PathVariable int yr, @PathVariable int mo, @PathVariable String sl) { ... }
B. @GetMapping("/blog/{year}/{month}/{slug}") public String getPost(@PathVariable("year") int y, @PathVariable("month") int m, @PathVariable("slug") String s) { ... }
C. @GetMapping("/blog/{year}/{month}/{slug}") public String getPost(@PathVariable String year, @PathVariable String month, @PathVariable String slug) { ... }
D. @GetMapping("/blog/{year}/{month}/{slug}") public String getPost(@PathVariable int y, @PathVariable int m, @PathVariable String s) { ... }

Solution

  1. Step 1: Match URL placeholders with method parameters

    The URL has {year}, {month}, {slug}. Method parameters must match these names or specify them explicitly.
  2. Step 2: Check parameter names and @PathVariable usage

    @GetMapping("/blog/{year}/{month}/{slug}") public String getPost(@PathVariable("year") int y, @PathVariable("month") int m, @PathVariable("slug") String s) { ... } uses different parameter names (y, m, s) but explicitly maps them to URL placeholders using @PathVariable("name"), which is correct.
  3. Final Answer:

    @GetMapping("/blog/{year}/{month}/{slug}") public String getPost(@PathVariable("year") int y, @PathVariable("month") int m, @PathVariable("slug") String s) { ... } -> Option B
  4. Quick Check:

    Explicit name mapping allows different parameter names [OK]
Hint: Use @PathVariable("name") if parameter name differs from URL placeholder [OK]
Common Mistakes:
  • Using mismatched parameter names without specifying name
  • Using wrong data types for year/month
  • Assuming parameter names must always match URL placeholders
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard