Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the @RestController annotation in Spring Boot?
easy
A. To create HTML views for web pages
B. To define a service class for business logic
C. To create a controller that returns data directly as JSON or XML
D. To configure database connections

Solution

  1. Step 1: Understand the role of @RestController

    @RestController is used to create REST APIs that return data directly, usually in JSON or XML format.
  2. Step 2: Compare with other options

    Creating HTML views is handled by MVC controllers, defining service classes by @Service, and configuring database connections elsewhere in Spring Boot.
  3. Final Answer:

    To create a controller that returns data directly as JSON or XML -> Option C
  4. Quick Check:

    @RestController = returns data directly [OK]
Hint: Remember: @RestController returns data, not HTML pages [OK]
Common Mistakes:
  • Confusing @RestController with @Service
  • Thinking it renders HTML views
  • Assuming it configures database
2. Which of the following is the correct way to declare a Spring Boot REST controller?
easy
A. @Service public class MyController {}
B. @Controller public class MyController {}
C. @RestService public class MyController {}
D. @RestController public class MyController {}

Solution

  1. Step 1: Identify the correct annotation for REST controllers

    @RestController is the correct annotation to create REST APIs in Spring Boot.
  2. Step 2: Check other annotations

    @Controller is for MVC controllers returning views, @RestService is not a valid annotation, and @Service is for service classes.
  3. Final Answer:

    @RestController public class MyController {} -> Option D
  4. Quick Check:

    Correct REST controller syntax = @RestController public class MyController {} [OK]
Hint: Use @RestController for REST APIs, not @Controller [OK]
Common Mistakes:
  • Using @Controller instead of @RestController
  • Using non-existent @RestService annotation
  • Confusing @Service with controller annotations
3. Given the following code, what will be the output when accessing /hello endpoint?
@RestController
public class HelloController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, Spring!";
    }
}
medium
A. An HTML page displaying "Hello, Spring!"
B. A plain text response with the string "Hello, Spring!"
C. A 404 Not Found error
D. A compilation error due to missing annotations

Solution

  1. Step 1: Understand @RestController behavior

    @RestController returns the method's return value directly as the response body, usually JSON or plain text.
  2. Step 2: Analyze the method return

    The method returns a String "Hello, Spring!" which will be sent as plain text in the response body.
  3. Final Answer:

    A plain text response with the string "Hello, Spring!" -> Option B
  4. Quick Check:

    @RestController + String return = plain text response [OK]
Hint: @RestController returns method data as response body [OK]
Common Mistakes:
  • Expecting an HTML page instead of data
  • Thinking it causes 404 without mapping
  • Assuming compilation error without reason
4. Identify the error in this Spring Boot REST controller code:
@RestController
public class UserController {
    @GetMapping("/user")
    public User getUser() {
        User user = new User();
        user.setName("Alice");
        return user;
    }
}
Assume User is a simple POJO with a name field and getters/setters.
medium
A. No error; code will return User object as JSON
B. User class must be annotated with @Entity
C. Missing @ResponseBody annotation on getUser() method
D. Missing @RequestMapping on class level

Solution

  1. Step 1: Check @RestController effect

    @RestController automatically adds @ResponseBody to all methods, so no need to add it explicitly.
  2. Step 2: Verify User class requirements

    User is a POJO; it does not need @Entity unless used with database. Returning it will serialize to JSON automatically.
  3. Final Answer:

    No error; code will return User object as JSON -> Option A
  4. Quick Check:

    @RestController auto @ResponseBody = No error; code will return User object as JSON [OK]
Hint: @RestController adds @ResponseBody automatically [OK]
Common Mistakes:
  • Adding unnecessary @ResponseBody on methods
  • Confusing @Entity with serialization
  • Thinking class-level @RequestMapping is mandatory
5. You want to create a REST API that returns a list of users as JSON. Which of the following code snippets correctly uses @RestController to achieve this?
hard
A. @RestController public class UserApi { @GetMapping("/users") public List<String> getUsers() { return List.of("Alice", "Bob", "Carol"); } }
B. @RestController public class UserApi { @GetMapping("/users") public String getUsers() { return "Alice, Bob, Carol"; } }
C. @Controller public class UserApi { @GetMapping("/users") public List<String> getUsers() { return List.of("Alice", "Bob", "Carol"); } }
D. @RestController public class UserApi { @RequestMapping("/users") public void getUsers() { System.out.println("Alice, Bob, Carol"); } }

Solution

  1. Step 1: Check @RestController and return type

    @RestController public class UserApi { @GetMapping("/users") public List<String> getUsers() { return List.of("Alice", "Bob", "Carol"); } } uses @RestController and returns a List<String>, which Spring Boot converts to JSON automatically.
  2. Step 2: Analyze other options

    The snippet returning String sends a plain string, not a JSON list. The snippet using @Controller attempts to resolve a view name, not return JSON data. The snippet with void return type sends no response body, only prints to console.
  3. Final Answer:

    @RestController public class UserApi { @GetMapping("/users") public List<String> getUsers() { return List.of("Alice", "Bob", "Carol"); } } -> Option A
  4. Quick Check:

    @RestController + List return = JSON list [OK]
Hint: Return List with @RestController for JSON array response [OK]
Common Mistakes:
  • Using @Controller instead of @RestController for JSON
  • Returning void or printing instead of returning data
  • Returning plain string instead of list for JSON array
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard