Bird
Raised Fist0
Spring Bootframework~20 mins

@RequestMapping for base paths in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
SpringBoot @RequestMapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the base URL path for this controller?
Given the following Spring Boot controller code, what is the base URL path that all methods inside this controller will respond to?
Spring Boot
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/users")
public class UserController {

    @RequestMapping("/list")
    public String listUsers() {
        return "User list";
    }

    @RequestMapping("/detail")
    public String userDetails() {
        return "User details";
    }
}
A/list
B/detail
C/api/v1/users/list
D/api/v1/users
Attempts:
2 left
💡 Hint
Look at the @RequestMapping annotation on the class level.
❓ state_output
intermediate
2:00remaining
What URL path does the listUsers() method respond to?
Using the same controller code, what is the full URL path that the listUsers() method will respond to?
Spring Boot
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/users")
public class UserController {

    @RequestMapping("/list")
    public String listUsers() {
        return "User list";
    }

    @RequestMapping("/detail")
    public String userDetails() {
        return "User details";
    }
}
A/api/v1/users/list
B/api/v1/users/detail
C/api/v1/users
D/list
Attempts:
2 left
💡 Hint
Combine the class-level and method-level @RequestMapping paths.
📝 Syntax
advanced
2:00remaining
Which option correctly sets a base path with multiple paths in @RequestMapping?
You want your controller to respond to two base paths: "/api/v1/users" and "/users". Which @RequestMapping annotation syntax correctly sets this?
A@RequestMapping({"/api/v1/users", "/users"})
B@RequestMapping("/api/v1/users", "/users")
C@RequestMapping(path = "/api/v1/users", path = "/users")
D@RequestMapping(value = "/api/v1/users" & "/users")
Attempts:
2 left
💡 Hint
Check how to specify multiple paths in an array for @RequestMapping.
🔧 Debug
advanced
2:00remaining
What error occurs with this incorrect @RequestMapping usage?
Consider this controller code snippet: @RestController @RequestMapping("/api/v1/users") public class UserController { @RequestMapping public String listUsers() { return "User list"; } @RequestMapping("/detail") public String userDetails() { return "User details"; } } What happens when you try to access "/api/v1/users" in the browser?
A500 Internal Server Error due to missing method path
BReturns "User list" because method-level @RequestMapping with no path matches base path
C404 Not Found error because method-level @RequestMapping requires a path
DCompilation error because @RequestMapping must have a path
Attempts:
2 left
💡 Hint
Method-level @RequestMapping without a path matches the base path.
🧠 Conceptual
expert
3:00remaining
How does @RequestMapping at class and method levels combine for HTTP methods?
Given this controller snippet: @RestController @RequestMapping(path = "/api/v1/items", method = RequestMethod.GET) public class ItemController { @RequestMapping(path = "/create", method = RequestMethod.POST) public String createItem() { return "Item created"; } @RequestMapping(path = "/list") public String listItems() { return "Item list"; } } Which HTTP methods and paths are valid for these two methods?
AcreateItem: POST /api/v1/items/create; listItems: POST /api/v1/items/list
BcreateItem: GET /api/v1/items/create; listItems: GET /api/v1/items/list
CcreateItem: POST /api/v1/items/create; listItems: GET /api/v1/items/list
DcreateItem: GET /api/v1/items/create; listItems: POST /api/v1/items/list
Attempts:
2 left
💡 Hint
Method-level @RequestMapping overrides HTTP method from class-level if specified.

Practice

(1/5)
1. What does placing @RequestMapping("/api") on a Spring Boot controller class do?
easy
A. Automatically maps all methods to the root URL /.
B. Defines the HTTP method type for all requests in the class.
C. Sets a base URL path for all request methods inside the class.
D. Disables URL mapping for the controller.

Solution

  1. Step 1: Understand class-level @RequestMapping

    When @RequestMapping is placed on a controller class, it sets a base URL path prefix for all methods inside.
  2. Step 2: Effect on method-level mappings

    Each method's path is appended to this base path, organizing URLs under a common root.
  3. Final Answer:

    Sets a base URL path for all request methods inside the class. -> Option C
  4. Quick Check:

    Class-level @RequestMapping sets base path = A [OK]
Hint: Class-level @RequestMapping sets base path prefix [OK]
Common Mistakes:
  • Confusing base path with HTTP method type
  • Thinking it maps all methods to root path
  • Assuming it disables mapping
2. Which of the following is the correct way to set a base path /users for a Spring Boot controller using @RequestMapping?
easy
A. @RequestMapping("/users") on each method only
B. @RequestMapping(path = "/users") on the class
C. @RequestMapping(method = "users") on the class
D. @RequestMapping(basePath = "/users") on the class

Solution

  1. Step 1: Check correct syntax for base path

    The correct attribute to set the path is path or directly the value in @RequestMapping. Using @RequestMapping(path = "/users") is valid and clear.
  2. Step 2: Validate other options

    @RequestMapping("/users") on each method only sets path only on methods, not base path. @RequestMapping(method = "users") on the class incorrectly uses method attribute for path. @RequestMapping(basePath = "/users") on the class uses a non-existent attribute basePath.
  3. Final Answer:

    @RequestMapping(path = "/users") on the class -> Option B
  4. Quick Check:

    Use path attribute with leading slash [OK]
Hint: Use path="/yourBase" with leading slash on class [OK]
Common Mistakes:
  • Using wrong attribute like method= for path
  • Using wrong attribute name like basePath
  • Setting base path only on methods
3. Given the controller class below, what is the full URL path to access the getUser() method?
@RequestMapping("/api")
public class UserController {
  @RequestMapping("/user")
  public String getUser() { return "user"; }
}
medium
A. /user
B. /user/api
C. /api
D. /api/user

Solution

  1. Step 1: Identify class-level base path

    The class has @RequestMapping("/api"), so all method paths start with /api.
  2. Step 2: Append method-level path

    The method getUser() has @RequestMapping("/user"), so the full path is /api + /user = /api/user.
  3. Final Answer:

    /api/user -> Option D
  4. Quick Check:

    Base path + method path = full URL [OK]
Hint: Combine class and method paths with slashes [OK]
Common Mistakes:
  • Ignoring class-level path
  • Mixing order of paths
  • Missing leading slash
4. What is wrong with this controller code?
@RequestMapping("api")
public class ProductController {
  @RequestMapping("/list")
  public String listProducts() { return "products"; }
}
medium
A. Class must be annotated with @RestController or @Controller.
B. Missing leading slash in class-level @RequestMapping path.
C. Method-level @RequestMapping cannot have a path.
D. Method must return ResponseEntity, not String.

Solution

  1. Step 1: Identify missing controller annotation

    The class is missing @Controller or @RestController. Without it, the class is not registered as a handler despite having @RequestMapping.
  2. Step 2: Validate other options

    Method-level paths are allowed. Leading slash is optional (Spring prepends it). Returning String is valid for view names or with @ResponseBody.
  3. Final Answer:

    Class must be annotated with @RestController or @Controller. -> Option A
  4. Quick Check:

    Controller annotation required [OK]
Hint: Use @RestController or @Controller on controller classes [OK]
Common Mistakes:
  • Missing @RestController or @Controller annotation
  • Thinking method paths are disallowed
  • Confusing @Controller with @RestController requirement
5. You want all endpoints in your controller to start with /shop, and inside you have a method mapped to /items. Which of these setups correctly maps the full URL /shop/items and why?
Option A:
@RequestMapping("/shop")
public class ShopController {
  @RequestMapping("/items")
  public String getItems() { return "items"; }
}

Option B:
@RequestMapping("/shop/items")
public class ShopController {
  @RequestMapping
  public String getItems() { return "items"; }
}

Option C:
@RequestMapping
public class ShopController {
  @RequestMapping("/shop/items")
  public String getItems() { return "items"; }
}

Option D:
@RequestMapping(shop)
public class ShopController {
  @RequestMapping(items)
  public String getItems() { return "items"; }
}
hard
A. Option A correctly sets base path and method path with leading slashes.
B. Option B sets full path on class, method has no path, works fine.
C. Option C sets full path on method only, class has no base path.
D. Option D misses leading slashes, so paths are invalid.

Solution

  1. Step 1: Identify the correct configuration

    The setup with class-level @RequestMapping("/shop") and method-level @RequestMapping("/items") combines to /shop/items with full syntax validity (parentheses, quotes, leading slashes).
  2. Step 2: Examine incorrect configurations

    The full class path setup lacks parentheses on method @RequestMapping (syntax error). The full method path setup lacks parentheses on class @RequestMapping (syntax error). The no-slash setup lacks quotes on shop and items (syntax error).
  3. Final Answer:

    Option A correctly sets base path and method path with leading slashes. -> Option A
  4. Quick Check:

    Base path + method path + valid syntax = full URL [OK]
Hint: Use leading slashes on class and method paths for clarity [OK]
Common Mistakes:
  • Missing parentheses in @RequestMapping
  • Missing quotes around path values
  • Omitting leading slashes on paths
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