Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set the base path for all requests in this controller.
Spring Boot
@RestController [1]("/api") public class ApiController { // methods here }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @GetMapping or @PostMapping at class level instead of @RequestMapping.
Forgetting to add the base path in the annotation.
✗ Incorrect
The @RequestMapping annotation at class level sets the base path for all methods in the controller.
2fill in blank
mediumComplete the code to map GET requests to the '/users' path under the base path.
Spring Boot
@RestController @RequestMapping("/api") public class UserController { @[1]("/users") public String getUsers() { return ""; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @PostMapping for GET requests.
Using @RequestMapping without specifying method.
✗ Incorrect
Use @GetMapping to handle GET requests for the '/users' endpoint under the base path.
3fill in blank
hardFix the error in the code to correctly map POST requests to '/submit' under the base path.
Spring Boot
@RestController @RequestMapping("/form") public class FormController { @RequestMapping(value = "/submit", method = RequestMethod.[1]) public String submitForm() { return "Submitted"; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for form submission.
Forgetting to specify the method attribute.
✗ Incorrect
The method attribute should be set to RequestMethod.POST to handle POST requests.
4fill in blank
hardFill both blanks to create a controller with base path '/shop' and map DELETE requests to '/item'.
Spring Boot
@RestController [1]("/shop") public class ShopController { @[2]("/item") public String deleteItem() { return "Item deleted"; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @GetMapping for DELETE requests.
Using @PostMapping at class level.
✗ Incorrect
Use @RequestMapping at class level for base path and @DeleteMapping for DELETE requests.
5fill in blank
hardFill all three blanks to define a controller with base path '/blog', map PUT requests to '/post', and GET requests to '/posts'.
Spring Boot
@RestController [1]("/blog") public class BlogController { @[2]("/post") public String updatePost() { return "Post updated"; } @[3]("/posts") public String getPosts() { return ""; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing HTTP method annotations.
Forgetting to set base path with @RequestMapping.
✗ Incorrect
Use @RequestMapping for base path, @PutMapping for PUT requests, and @GetMapping for GET requests.