Bird
Raised Fist0
Spring Bootframework~20 mins

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

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

Practice

(1/5)
1. Why are REST controllers essential in a Spring Boot application?
easy
A. They manage database connections directly.
B. They style the user interface of the application.
C. They handle HTTP requests and return data in formats like JSON.
D. They compile the Java code into bytecode.

Solution

  1. Step 1: Understand the role of REST controllers

    REST controllers are designed to handle web requests and responses in a Spring Boot app.
  2. Step 2: Identify what REST controllers return

    They typically return data in formats like JSON, which is easy for other apps to use.
  3. Final Answer:

    They handle HTTP requests and return data in formats like JSON. -> Option C
  4. Quick Check:

    REST controllers = handle HTTP + return JSON [OK]
Hint: REST controllers handle web requests and send JSON responses [OK]
Common Mistakes:
  • Thinking REST controllers manage database connections
  • Confusing REST controllers with UI styling components
  • Believing REST controllers compile code
2. Which annotation is used to define a REST controller in Spring Boot?
easy
A. @RestController
B. @Controller
C. @Service
D. @Repository

Solution

  1. Step 1: Recall Spring Boot annotations for controllers

    @Controller is for MVC controllers, but REST controllers need to return data directly.
  2. Step 2: Identify the annotation that combines @Controller and @ResponseBody

    @RestController does both, making it perfect for REST APIs.
  3. Final Answer:

    @RestController -> Option A
  4. Quick Check:

    @RestController = REST API controller [OK]
Hint: Use @RestController to create REST APIs in Spring Boot [OK]
Common Mistakes:
  • Using @Controller without @ResponseBody for REST APIs
  • Confusing @Service or @Repository as controller annotations
  • Omitting the annotation entirely
3. What will be the output when accessing the endpoint defined by this REST controller method?
@RestController
public class HelloController {
  @GetMapping("/hello")
  public String sayHello() {
    return "Hello, World!";
  }
}
medium
A. A JSON object {"message":"Hello, World!"}
B. Plain text response: Hello, World!
C. HTTP 404 Not Found error
D. An HTML page with 'Hello, World!' text

Solution

  1. Step 1: Analyze the method return type and annotation

    The method returns a String and is inside a @RestController, so the string is sent as plain text.
  2. Step 2: Understand @RestController behavior

    @RestController automatically adds @ResponseBody, so the return is the response body, not a view or JSON object.
  3. Final Answer:

    Plain text response: Hello, World! -> Option B
  4. Quick Check:

    String return in @RestController = plain text response [OK]
Hint: String return in @RestController sends plain text response [OK]
Common Mistakes:
  • Expecting JSON output without wrapping string in an object
  • Thinking it returns an HTML page
  • Assuming 404 error without mapping
4. Identify the error in this REST controller code snippet:
@RestController
public class UserController {
  @GetMapping("/user")
  public User getUser() {
    User user = new User("Alice", 30);
    return user;
  }
}

Assuming User is a simple POJO with name and age fields.
medium
A. No error; this code will return user data as JSON
B. User class must be annotated with @Entity
C. The method should return String, not User
D. Missing @ResponseBody annotation on the method

Solution

  1. Step 1: Check if @RestController handles response body automatically

    @RestController combines @Controller and @ResponseBody, so no need to add @ResponseBody on methods.
  2. Step 2: Confirm if returning a POJO works

    Spring Boot automatically converts POJOs to JSON in REST responses.
  3. Final Answer:

    No error; this code will return user data as JSON -> Option A
  4. Quick Check:

    @RestController + POJO return = JSON response [OK]
Hint: @RestController auto-converts POJOs to JSON, no extra annotation needed [OK]
Common Mistakes:
  • Adding unnecessary @ResponseBody on methods inside @RestController
  • Thinking POJO must be @Entity for REST response
  • Changing return type to String unnecessarily
5. You want to create a REST controller that returns a list of users as JSON. Which approach correctly ensures the response is JSON and accessible at "/users" endpoint?
@RestController
public class UserController {
  public List<User> getUsers() {
    return List.of(new User("Bob", 25), new User("Carol", 28));
  }
}
hard
A. Return a String with JSON manually instead of List<User>
B. Change @RestController to @Controller and add @ResponseBody on getUsers()
C. Annotate User class with @RestController
D. Add @GetMapping("/users") above getUsers() method

Solution

  1. Step 1: Identify missing mapping annotation

    The method lacks @GetMapping, so it won't be exposed as an endpoint.
  2. Step 2: Confirm correct annotation for REST endpoint

    Adding @GetMapping("/users") exposes the method at /users and returns JSON automatically.
  3. Final Answer:

    Add @GetMapping("/users") above getUsers() method -> Option D
  4. Quick Check:

    @GetMapping + @RestController = JSON endpoint [OK]
Hint: Use @GetMapping on method inside @RestController for JSON endpoint [OK]
Common Mistakes:
  • Forgetting @GetMapping annotation on methods
  • Misusing @Controller without @ResponseBody
  • Trying to manually build JSON strings
  • Annotating POJO class with @RestController
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