0
0
Spring Bootframework~20 mins

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

Choose your learning style9 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.