0
0
Spring Bootframework~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
REST Controller Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use REST controllers in Spring Boot?
What is the main purpose of REST controllers in a Spring Boot application?
ATo style the user interface with CSS
BTo manage database connections directly
CTo handle HTTP requests and send responses in a RESTful way
DTo compile Java code into bytecode
Attempts:
2 left
💡 Hint
Think about how web applications communicate with clients over the internet.
component_behavior
intermediate
2:00remaining
Behavior of a REST controller method
Given a Spring Boot REST controller method annotated with @GetMapping("/hello"), what will be the output when a client sends a GET request to /hello?
Spring Boot
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}
AThe client receives the plain text response: Hello, World!
BThe client receives a 404 Not Found error
CThe client receives an HTML page with the text Hello, World!
DThe client receives a JSON object with key 'message' and value 'Hello, World!'
Attempts:
2 left
💡 Hint
Look at the return type and annotations used.
📝 Syntax
advanced
2:00remaining
Correct annotation for REST controller
Which annotation correctly defines a REST controller in Spring Boot that returns JSON responses by default?
A@Controller
B@RestController
C@Service
D@Component
Attempts:
2 left
💡 Hint
This annotation combines @Controller and @ResponseBody.
🔧 Debug
advanced
2:00remaining
Why does this REST controller method return 404?
Consider this Spring Boot REST controller method: @GetMapping("/greet") public String greet() { return "Greetings!"; } Why might a GET request to /greet return a 404 Not Found error?
Spring Boot
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetController {
    @GetMapping("/greet")
    public String greet() {
        return "Greetings!";
    }
}
AThe application is not running or the controller is not scanned by Spring Boot
BThe class is missing the @RestController annotation
CThe method should return a ResponseEntity<String> instead of String
DThe @GetMapping annotation should be @PostMapping
Attempts:
2 left
💡 Hint
Check if Spring Boot knows about this controller class.
state_output
expert
3:00remaining
Output of REST controller with path variable and JSON response
What is the JSON output of this Spring Boot REST controller method when a client sends a GET request to /user/42? @GetMapping("/user/{id}") public Map getUser(@PathVariable int id) { return Map.of("userId", id, "name", "User" + id); }
Spring Boot
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

@RestController
public class UserController {
    @GetMapping("/user/{id}")
    public Map<String, Object> getUser(@PathVariable int id) {
        return Map.of("userId", id, "name", "User" + id);
    }
}
A404 Not Found error
B"userId=42,name=User42"
C{"id":42,"username":"User42"}
D{"userId":42,"name":"User42"}
Attempts:
2 left
💡 Hint
Look at the keys and values returned in the Map and how Spring Boot converts it to JSON.