0
0
Spring Bootframework~10 mins

Why REST controllers are essential in Spring Boot - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a REST controller in Spring Boot.

Spring Boot
@[1]
public class MyController {
}
Drag options to blanks, or click blank then click option'
ARepository
BService
CComponent
DRestController
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service instead of @RestController
Forgetting the @RestController annotation
Using @Component which is too generic
2fill in blank
medium

Complete the code to map a GET request to the method.

Spring Boot
@GetMapping("/[1]")
public String greet() {
    return "Hello";
}
Drag options to blanks, or click blank then click option'
Ahello
Bpost
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using HTTP method names like 'post' in a GET mapping
Leaving the path empty when a path is needed
Using incorrect HTTP method annotations
3fill in blank
hard

Fix the error in the method signature to accept a path variable.

Spring Boot
@GetMapping("/user/[1]")
public String getUser(@PathVariable String id) {
    return "User " + id;
}
Drag options to blanks, or click blank then click option'
A:id
B{id}
C<id>
D(id)
Attempts:
3 left
💡 Hint
Common Mistakes
Using colon or angle brackets instead of curly braces
Not matching the path variable name with the method parameter
Omitting the @PathVariable annotation
4fill in blank
hard

Fill both blanks to create a POST endpoint that accepts JSON data.

Spring Boot
@PostMapping("/[1]")
public ResponseEntity<String> createItem(@[2] Item item) {
    return ResponseEntity.ok("Created");
}
Drag options to blanks, or click blank then click option'
Aitems
BRequestBody
CPathVariable
DRequestParam
Attempts:
3 left
💡 Hint
Common Mistakes
Using @PathVariable or @RequestParam instead of @RequestBody for JSON data
Using singular resource name for POST endpoint
Omitting the annotation for the method parameter
5fill in blank
hard

Fill all three blanks to return a JSON map with filtered data.

Spring Boot
Map<String, Integer> result = items.stream()
    .filter(item -> item.getQuantity() [1] 10)
    .collect(Collectors.toMap(
        item -> item.getName().[2](),
        item -> item.getQuantity() [3] 1
    ));
Drag options to blanks, or click blank then click option'
A>
BtoUpperCase
C+
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in filter
Using toLowerCase() instead of toUpperCase()
Subtracting instead of adding in the map value