0
0
Spring Bootframework~20 mins

@RestController annotation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Boot @RestController Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot controller method?
Consider this Spring Boot controller class annotated with @RestController. What will be the HTTP response body when accessing /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 HTTP response will be a redirect to /hello.html
BThe HTTP response will be a JSON object {"message": "Hello, World!"}
CThe HTTP response will be empty with status 200 OK
DThe HTTP response body will be the string "Hello, World!"
Attempts:
2 left
💡 Hint
Remember that @RestController combines @Controller and @ResponseBody.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses @RestController to handle a POST request?
Select the code snippet that correctly defines a Spring Boot controller method to handle POST requests at path /submit and returns a confirmation string.
A
@RestController
public class SubmitController {
    @GetMapping("/submit")
    public String submit() {
        return "Submitted!";
    }
}
B
@RestController
public class SubmitController {
    @PostMapping("/submit")
    public String submit() {
        return "Submitted!";
    }
}
C
@Controller
public class SubmitController {
    @PostMapping("/submit")
    public String submit() {
        return "Submitted!";
    }
}
D
@RestController
public class SubmitController {
    @PostMapping("submit")
    public String submit() {
        return "Submitted!";
    }
}
Attempts:
2 left
💡 Hint
Check the HTTP method annotation and the path format.
🔧 Debug
advanced
2:00remaining
Why does this @RestController method return a 404 error?
Given this controller, why does accessing /api/data return 404 Not Found?
Spring Boot
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DataController {
    @GetMapping("api/data")
    public String getData() {
        return "Data";
    }
}
AThe path in @GetMapping is missing a leading slash; it should be "/api/data"
BThe method must return a ResponseEntity<String> instead of String
CThe class must be annotated with @Controller instead of @RestController
DThe method must be public static to be accessible
Attempts:
2 left
💡 Hint
Check the path string format in the mapping annotation.
🧠 Conceptual
advanced
2:00remaining
What is the main difference between @Controller and @RestController?
Choose the statement that best describes the difference between @Controller and @RestController in Spring Boot.
A@RestController combines @Controller and @ResponseBody, so methods return data directly as HTTP response body
B@Controller automatically serializes return values to JSON, while @RestController does not
C@RestController is used only for WebSocket endpoints, @Controller for HTTP
D@Controller is deprecated and replaced by @RestController in Spring Boot
Attempts:
2 left
💡 Hint
Think about how return values are handled in HTTP responses.
state_output
expert
2:00remaining
What is the HTTP response when this @RestController method throws an exception?
Given this controller method, what will be the HTTP response status and body when /error is accessed?
Spring Boot
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ErrorController {
    @GetMapping("/error")
    public String error() {
        throw new IllegalStateException("Something went wrong");
    }
}
AHTTP 500 Internal Server Error with default error HTML page
BHTTP 200 OK with body "Something went wrong"
CHTTP 500 Internal Server Error with JSON body containing error details
DHTTP 404 Not Found with empty body
Attempts:
2 left
💡 Hint
Spring Boot's default error handling for REST controllers returns JSON error responses.