Bird
Raised Fist0
Spring Bootframework~20 mins

Request mapping by method and path in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Request Mapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the HTTP response status when accessing the mapped endpoint?
Consider this Spring Boot controller method:
@RestController
public class MyController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String greet() {
        return "Hello World";
    }
}

What will be the HTTP status code when a client sends a GET request to /hello?
Spring Boot
@RestController
public class MyController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String greet() {
        return "Hello World";
    }
}
A200 OK
B404 Not Found
C500 Internal Server Error
D405 Method Not Allowed
Attempts:
2 left
💡 Hint
The method matches the GET request for the path exactly.
📝 Syntax
intermediate
2:00remaining
Which option correctly maps POST requests to /submit?
You want to create a Spring Boot controller method that handles POST requests to the path /submit. Which of the following method annotations is correct?
A@RequestMapping(path = "/submit", method = RequestMethod.GET)
B@RequestMapping(value = "/submit", method = RequestMethod.POST)
C@RequestMapping(value = "/submit")
D@RequestMapping(method = RequestMethod.POST)
Attempts:
2 left
💡 Hint
You need to specify both the path and the POST method explicitly.
🔧 Debug
advanced
2:00remaining
Why does this controller method cause a 405 error on POST /data?
Given this controller:
@RestController
public class DataController {
    @RequestMapping(value = "/data", method = RequestMethod.GET)
    public String getData() {
        return "Data";
    }
}

When a client sends a POST request to /data, the server responds with 405 Method Not Allowed. Why?
Spring Boot
@RestController
public class DataController {
    @RequestMapping(value = "/data", method = RequestMethod.GET)
    public String getData() {
        return "Data";
    }
}
ABecause the controller is missing @PostMapping annotation
BBecause the method throws an exception on POST requests
CBecause the path /data is invalid
DBecause there is no method mapped to POST requests at /data
Attempts:
2 left
💡 Hint
Check which HTTP methods are allowed for the path.
🧠 Conceptual
advanced
2:00remaining
What happens if two methods map to the same path but different HTTP methods?
In a Spring Boot controller, you have:
@RequestMapping(value = "/item", method = RequestMethod.GET)
public String getItem() { return "Get"; }

@RequestMapping(value = "/item", method = RequestMethod.POST)
public String postItem() { return "Post"; }

What happens when a client sends a GET request and a POST request to /item?
Spring Boot
@RequestMapping(value = "/item", method = RequestMethod.GET)
public String getItem() { return "Get"; }

@RequestMapping(value = "/item", method = RequestMethod.POST)
public String postItem() { return "Post"; }
AGET requests return "Get" and POST requests return "Post"
BBoth GET and POST requests return "Get"
CBoth GET and POST requests return "Post"
DRequests cause a conflict error at startup
Attempts:
2 left
💡 Hint
Spring Boot matches methods by both path and HTTP method.
❓ state_output
expert
2:00remaining
What is the response body when sending a PUT request to /update with this controller?
Given this Spring Boot controller:
@RestController
public class UpdateController {
    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.PUT})
    public String update() {
        return "Updated";
    }
}

What will be the response body when a client sends a PUT request to /update?
Spring Boot
@RestController
public class UpdateController {
    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.PUT})
    public String update() {
        return "Updated";
    }
}
A"Method Not Allowed"
B"Not Found"
C"Updated"
DEmpty response body
Attempts:
2 left
💡 Hint
The method handles both POST and PUT requests.

Practice

(1/5)
1. What does the @RequestMapping annotation do in Spring Boot?
easy
A. It styles the HTML page returned by the controller.
B. It creates a new database connection automatically.
C. It links a URL path and HTTP method to a specific method in a controller.
D. It schedules background tasks to run periodically.

Solution

  1. Step 1: Understand the purpose of @RequestMapping

    @RequestMapping is used to connect web requests to controller methods in Spring Boot.
  2. Step 2: Identify what it links

    It links both the URL path and the HTTP method (GET, POST, etc.) to the method that should handle the request.
  3. Final Answer:

    It links a URL path and HTTP method to a specific method in a controller. -> Option C
  4. Quick Check:

    @RequestMapping links URL and method [OK]
Hint: Remember: @RequestMapping connects URL + HTTP method to code [OK]
Common Mistakes:
  • Thinking @RequestMapping styles HTML output
  • Confusing it with database or scheduling annotations
  • Assuming it only maps URL without HTTP method
2. Which of the following is the correct way to map a GET request to path "/home" using @RequestMapping?
easy
A. @RequestMapping(route = "/home", method = GET)
B. @RequestMapping(path = "/home", method = RequestMethod.GET)
C. @RequestMapping(value = "/home", type = "GET")
D. @RequestMapping(method = "GET", url = "/home")

Solution

  1. Step 1: Recall correct @RequestMapping syntax

    The correct syntax uses 'path' or 'value' for URL and 'method' with RequestMethod enum for HTTP method.
  2. Step 2: Match options with syntax

    @RequestMapping(path = "/home", method = RequestMethod.GET) uses path and method=RequestMethod.GET correctly. Others use invalid attribute names or wrong types.
  3. Final Answer:

    @RequestMapping(path = "/home", method = RequestMethod.GET) -> Option B
  4. Quick Check:

    Correct syntax [OK]
Hint: Use 'path' and RequestMethod.GET for GET mapping [OK]
Common Mistakes:
  • Using 'url' instead of 'path' or 'value'
  • Passing HTTP method as string instead of RequestMethod enum
  • Using wrong attribute names like 'route' or 'type'
3. Given this controller method:
@RequestMapping(path = "/test", method = RequestMethod.POST)
public String test() {
    return "Success";
}
What happens when a client sends a GET request to "/test"?
medium
A. The server crashes with an exception.
B. The method runs and returns "Success".
C. Spring returns a 404 Not Found error.
D. Spring returns a 405 Method Not Allowed error.

Solution

  1. Step 1: Identify the mapped HTTP method

    The method is mapped only to POST requests at path "/test".
  2. Step 2: Understand what happens on GET request

    A GET request to the same path is not handled, so Spring returns 405 Method Not Allowed.
  3. Final Answer:

    Spring returns a 405 Method Not Allowed error. -> Option D
  4. Quick Check:

    GET to POST-only path = 405 error [OK]
Hint: GET request to POST-only path causes 405 error [OK]
Common Mistakes:
  • Assuming GET runs the POST method
  • Thinking it returns 404 Not Found instead
  • Believing server crashes on wrong method
4. What is wrong with this mapping?
@RequestMapping(path = "/data", method = "POST")
public String saveData() {
    return "Saved";
}
medium
A. The method attribute should use RequestMethod.POST, not a string.
B. The path attribute cannot be used with @RequestMapping.
C. The method must return void, not String.
D. The method name must be saveDataPost, not saveData.

Solution

  1. Step 1: Check method attribute type

    The method attribute expects a RequestMethod enum value, not a string.
  2. Step 2: Identify correct usage

    Using method = "POST" is invalid; it should be method = RequestMethod.POST.
  3. Final Answer:

    The method attribute should use RequestMethod.POST, not a string. -> Option A
  4. Quick Check:

    method attribute needs RequestMethod enum [OK]
Hint: Use RequestMethod.POST enum, not string "POST" [OK]
Common Mistakes:
  • Passing HTTP method as string instead of enum
  • Thinking path attribute is invalid
  • Assuming return type or method name causes error
5. You want to create two methods in a controller: one handles GET requests at "/items" and another handles POST requests at the same path "/items". Which is the best way to do this?
hard
A. Use two methods with @RequestMapping(path = "/items", method = RequestMethod.GET) and @RequestMapping(path = "/items", method = RequestMethod.POST).
B. Use one method with @RequestMapping(path = "/items") and check HTTP method inside the method manually.
C. Use @RequestMapping(path = "/items") on both methods without specifying method attribute.
D. Use @RequestMapping(method = RequestMethod.GET) on one method and @RequestMapping(method = RequestMethod.POST) on the other without path.

Solution

  1. Step 1: Understand how to map same path with different methods

    Spring allows multiple methods mapped to the same path but different HTTP methods using method attribute.
  2. Step 2: Evaluate options

    Use two methods with @RequestMapping(path = "/items", method = RequestMethod.GET) and @RequestMapping(path = "/items", method = RequestMethod.POST). correctly uses two methods with same path but different HTTP methods. Use one method with @RequestMapping(path = "/items") and check HTTP method inside the method manually. is less clean and error-prone. Use @RequestMapping(path = "/items") on both methods without specifying method attribute. causes ambiguity. Use @RequestMapping(method = RequestMethod.GET) on one method and @RequestMapping(method = RequestMethod.POST) on the other without path. misses path, so won't map correctly.
  3. Final Answer:

    Use two methods with @RequestMapping(path = "/items", method = RequestMethod.GET) and @RequestMapping(path = "/items", method = RequestMethod.POST). -> Option A
  4. Quick Check:

    Separate methods with same path, different HTTP methods [OK]
Hint: Map same path with different methods by specifying method attribute [OK]
Common Mistakes:
  • Not specifying method attribute causing conflicts
  • Checking HTTP method manually inside one method
  • Omitting path attribute causing mapping errors
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