Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What does @RestController add compared to @Controller, and what do @GetMapping and @RequestMapping do?

@RestController is @Controller plus @ResponseBody, so every handler method's return value is written straight to the HTTP response body by a message converter, usually as JSON, instead of being treated as a view name. @RequestMapping maps requests to a handler by path and by conditions such as method, headers or content types; @GetMapping is a shortcut for @RequestMapping(method = GET). On the class, @RequestMapping sets a path prefix for all methods.

Why this question is asked

These annotations are the first thing we write in any Spring web service, so it is easy to use them without knowing what they change. Interviewers ask this to check that we understand the difference between rendering a view and writing a body, and how Spring decides which method handles a request. In this answer, we will recap what @Controller does on its own, see exactly what @RestController adds, follow a return value through the message converters, and then look at how @RequestMapping and its shortcuts match requests.

1A Quick Recap of @Controller

@Controller marks a class as a web controller. It is a stereotype annotation, meaning it is itself annotated with @Component, so component scanning registers the class as a bean. Spring MVC's DispatcherServlet, the front controller that receives every request, then looks at its methods for request mappings.

By default, what a @Controller method returns is a view name. Returning "orders" tells Spring to find a template called orders, for example orders.html with Thymeleaf, and render it with the model. That is the classic server-rendered page.

Java
@Controller
public class PageController {
@GetMapping("/orders")
public String orders(Model model) {
model.addAttribute("orders", List.of("A-1", "A-2"));
return "orders"; // a view name: renders templates/orders.html
}
}

2What @RestController Adds

@RestController is a composed annotation. If we open its source, it is annotated with @Controller and @ResponseBody. @ResponseBody on a class means every method behaves as if it had @ResponseBody itself: the return value is the response body, not a view name.

Java
@RestController // same as @Controller + @ResponseBody
public class OrderApi {
@GetMapping("/api/orders/{id}")
public OrderDto get(@PathVariable long id) {
return new OrderDto(id, "NEW"); // written as JSON: {"id":42,"status":"NEW"}
}
@GetMapping("/api/ping")
public String ping() {
return "orders"; // the text orders, not a template
}
}

Notice the second method. With @Controller, returning "orders" would render a template; with @RestController, the client receives the plain text orders. This is the most common source of confusion when someone copies a method between the two kinds of class.

3How the Return Value Becomes JSON

Writing the body is the job of an HttpMessageConverter, a small strategy object that knows how to turn a Java object into bytes for one media type, and back. Spring Boot registers several: one for Strings, one for byte arrays, and a Jackson converter for JSON when Jackson is on the classpath, which spring-boot-starter-web includes.

Spring picks the converter through content negotiation. It looks at the Accept header of the request and at the return type, and chooses the first converter that can write that type in an acceptable media type. If the client sends Accept: application/json, the Jackson converter writes the OrderDto. If no converter can produce what the client asked for, Spring answers 406 Not Acceptable.

Step throughFrom handler method to JSON response
ClientDispatcherServletOrderApiMessage converter
GET /api/orders/42, Accept: application/json
call get(42)
OrderDto(42, NEW)
find converter for OrderDto and JSON
200 {"id":42,"status":"NEW"}

5 messages in order. Play them, or step through them one at a time.

Because of @ResponseBody, the return value skips view resolution and goes to a converter.

4@RequestMapping and Its Shortcuts

@RequestMapping tells Spring which requests a method handles. The path is the main condition, but it can also narrow by HTTP method, by request parameters, by headers, by consumes (the Content-Type the method accepts) and by produces (the media type it returns). A request must match all conditions to reach the method.

@GetMapping, @PostMapping, @PutMapping, @PatchMapping and @DeleteMapping are shortcuts: each is @RequestMapping with the method already set. They make code shorter and clearer, so we use them on methods. Spring Framework 7 adds a version attribute to mappings for API versioning; check your version for the details.

Java
@RestController
@RequestMapping("/api/orders") // prefix for every method below
public class OrderApi {
@GetMapping("/{id}") // GET /api/orders/42
public OrderDto get(@PathVariable long id) { ... }
// Long form of the same mapping (declaring both would fail at startup as ambiguous):
// @RequestMapping(path = "/{id}", method = RequestMethod.GET)
@PostMapping(consumes = "application/json") // POST /api/orders with a JSON body
public OrderDto create(@RequestBody CreateOrder req) { ... }
}
// POST /api/orders with Content-Type: text/plain -> 415 Unsupported Media Type
// DELETE /api/orders/42 -> 405 Method Not Allowed (path matches, method does not)

Notice the class-level @RequestMapping("/api/orders"). It sets a common prefix, and each method path is appended to it. This keeps related endpoints together and avoids repeating the base path. Also notice the comment about the long form: if two methods map the same path and conditions, Spring fails at startup with an ambiguous mapping error, which protects us from silent conflicts.

5Follow-up questions

Can a @Controller return JSON?

Yes. Add @ResponseBody to the method, or return ResponseEntity<T>, which is always written as a body. This is useful in a mostly server-rendered controller that has one or two JSON endpoints for a page script.

What happens when the path matches but the method does not?

Spring finds that some mapping matched the path but none matched the HTTP method, and answers 405 Method Not Allowed with an Allow header listing the supported methods. If the path matches nothing at all, the answer is 404.

Do I still need @ResponseBody on methods of a @RestController?

No. It is already inherited from the class. Adding it again does nothing harmful but is noise.

6Conclusion

In this answer, we saw that @RestController is simply @Controller with @ResponseBody, so return values are written to the response body by HttpMessageConverters chosen through content negotiation, instead of being resolved as view names. @RequestMapping maps requests by path and conditions such as method, consumes and produces, @GetMapping and its siblings are shortcuts with the method preset, and a class-level @RequestMapping provides a common path prefix.

What to say in the interviewAbout 80 seconds aloud

Open with

@RestController is a composed annotation: it's @Controller plus @ResponseBody. With a plain @Controller, a method's return value is treated as a view name, so returning "orders" renders the orders template.

Then, if they want more

With @ResponseBody on the class, every method's return value becomes the response body instead. Spring hands it to an HttpMessageConverter, chosen by content negotiation from the return type and the Accept header, and with Jackson on the classpath an object is written as JSON.

If no converter can produce what the client accepts, you get 406. @RequestMapping is the general mapping annotation.

It matches a request by path and can add conditions like the HTTP method, consumes, produces, headers or params, and all conditions have to match. @GetMapping is just @RequestMapping with method set to GET, and there are matching shortcuts for POST, PUT, PATCH and DELETE, which I use on methods because they're clearer.

On the class, @RequestMapping sets a common prefix, so with /api/orders on the class and /{id} on the method, the full path is /api/orders/{id}. One detail I like to mention: if the path matches but the method doesn't, Spring answers 405 rather than 404, and duplicate mappings fail at startup.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does @RestController add compared to @Controller, and what do @GetMapping and...": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/ac416b1c23/

More in REST controllers