What is the purpose of the @RestController annotation in Spring Boot?
The <code>@RestController</code> annotation marks a class as a controller where every method returns a domain object instead of a view. It combines <code>@Controller</code> and <code>@ResponseBody</code>, making it easier to create RESTful web services.
Click to reveal answer
beginner
How does @RestController differ from @Controller in Spring Boot?
@RestController automatically serializes return objects to JSON or XML and sends them as HTTP responses. @Controller is used for web pages and requires explicit @ResponseBody to return data directly.
Click to reveal answer
beginner
Can you use @RestController on multiple classes in a Spring Boot application?
Yes, you can use <code>@RestController</code> on multiple classes. Each class can handle different REST endpoints, helping organize your API logically.
Click to reveal answer
intermediate
What HTTP response content type does @RestController typically produce?
@RestController typically produces JSON by default, but it can also produce XML or other formats if configured with message converters.
Click to reveal answer
beginner
Why is @RestController considered a convenience annotation?
Because it combines @Controller and @ResponseBody, reducing boilerplate code and making REST API development simpler and cleaner.
Click to reveal answer
What does the @RestController annotation do in Spring Boot?
AMarks a class as a view resolver for HTML pages
BMarks a class as a REST API controller that returns data directly
CDefines a database entity
DConfigures security settings
✗ Incorrect
@RestController is used to create REST APIs by returning data directly as JSON or XML.
Which two annotations does @RestController combine?
A<code>@Controller</code> and <code>@ResponseBody</code>
B<code>@Service</code> and <code>@Repository</code>
C<code>@Component</code> and <code>@Entity</code>
D<code>@RequestMapping</code> and <code>@PathVariable</code>
✗ Incorrect
@RestController is a shortcut for @Controller plus @ResponseBody.
What is the default format of data returned by methods in a @RestController?
AHTML
BCSV
CPlain text
DJSON
✗ Incorrect
By default, Spring Boot converts returned objects to JSON in @RestController methods.
If you want to return a web page instead of data, which annotation should you use instead of @RestController?
A<code>@Controller</code>
B<code>@Service</code>
C<code>@Repository</code>
D<code>@Component</code>
✗ Incorrect
@Controller is used for returning views like HTML pages.
Can a Spring Boot application have multiple classes annotated with @RestController?
AOnly if they extend a common base class
BNo, only one is allowed
CYes, to organize different API endpoints
DOnly if they are in different packages
✗ Incorrect
Multiple @RestController classes help organize APIs logically.
Explain what the @RestController annotation does and why it is useful in Spring Boot applications.
Think about how it simplifies returning data from web endpoints.
You got /4 concepts.
Describe the difference between @RestController and @Controller in Spring Boot.
Focus on what each annotation returns to the client.
You got /4 concepts.
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
Step 1: Understand the role of @RestController
@RestController is used to create REST APIs that return data directly, usually in JSON or XML format.
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.
Final Answer:
To create a controller that returns data directly as JSON or XML -> Option C
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
Step 1: Identify the correct annotation for REST controllers
@RestController is the correct annotation to create REST APIs in Spring Boot.
Step 2: Check other annotations
@Controller is for MVC controllers returning views, @RestService is not a valid annotation, and @Service is for service classes.
Final Answer:
@RestController public class MyController {} -> Option D
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
Step 1: Understand @RestController behavior
@RestController returns the method's return value directly as the response body, usually JSON or plain text.
Step 2: Analyze the method return
The method returns a String "Hello, Spring!" which will be sent as plain text in the response body.
Final Answer:
A plain text response with the string "Hello, Spring!" -> Option B
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
Step 1: Check @RestController effect
@RestController automatically adds @ResponseBody to all methods, so no need to add it explicitly.
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.
Final Answer:
No error; code will return User object as JSON -> Option A
Quick Check:
@RestController auto @ResponseBody = No error; code will return User object as JSON [OK]
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
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.
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.
Final Answer:
@RestController public class UserApi { @GetMapping("/users") public List<String> getUsers() { return List.of("Alice", "Bob", "Carol"); } } -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.