0
0
Spring Bootframework~10 mins

@RestController annotation in Spring Boot - Interactive Code Practice

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 class in Spring Boot.

Spring Boot
import org.springframework.web.bind.annotation.[1];

@[1]
public class HelloController {
    // controller methods
}
Drag options to blanks, or click blank then click option'
AService
BRestController
CComponent
DRepository
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service or @Component instead of @RestController
Forgetting to import the annotation
2fill in blank
medium

Complete the code to create a GET endpoint that returns a greeting string.

Spring Boot
@RestController
public class GreetingController {

    @[1]("/greet")
    public String greet() {
        return "Hello!";
    }
}
Drag options to blanks, or click blank then click option'
AGetMapping
BPostMapping
CRequestMapping
DDeleteMapping
Attempts:
3 left
💡 Hint
Common Mistakes
Using @PostMapping for a GET request
Not specifying the path in the annotation
3fill in blank
hard

Fix the error in the code by completing the annotation to handle POST requests.

Spring Boot
@RestController
public class DataController {

    @[1]("/data")
    public String saveData() {
        return "Data saved";
    }
}
Drag options to blanks, or click blank then click option'
APatchMapping
BPutMapping
CGetMapping
DPostMapping
Attempts:
3 left
💡 Hint
Common Mistakes
Using @GetMapping for POST requests
Using incorrect HTTP method annotations
4fill in blank
hard

Fill both blanks to create a REST controller with a GET endpoint returning JSON.

Spring Boot
import org.springframework.web.bind.annotation.[1];
import org.springframework.web.bind.annotation.[2];

@[1]
public class UserController {

    @[2]("/user")
    public User getUser() {
        return new User("Alice", 30);
    }
}
Drag options to blanks, or click blank then click option'
ARestController
BService
CGetMapping
DPostMapping
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service instead of @RestController
Using @PostMapping for a GET endpoint
5fill in blank
hard

Fill both blanks to create a REST controller with a POST endpoint that accepts JSON and returns a confirmation string.

Spring Boot
import org.springframework.web.bind.annotation.[1];
import org.springframework.web.bind.annotation.[2];
import org.springframework.web.bind.annotation.RequestBody;

@[1]
public class OrderController {

    @[2]("/order")
    public String placeOrder(@RequestBody Order order) {
        return "Order placed for " + order.getItem();
    }
}
Drag options to blanks, or click blank then click option'
ARestController
BPostMapping
CGetMapping
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @GetMapping for POST requests
Forgetting @RequestBody for JSON input