SteelReed

Search

Search posts, questions…

Spring BootQuestion 13 / 26

Junior

How do @Controller and @RestController differ? What are redirect, forward and Post/Redirect/Get?

Short answer

@RestController = @Controller + @ResponseBody: the return value is written straight into the body (JSON); with @Controller, the returned string is a view name. Post/Redirect/Get means redirecting to a GET after a POST, so F5 doesn't resubmit the form.

returns a string@ResponseBodyredirect: 302@ControllerView name, render template@RestControllerJSON written to bodyPOST formGET result pageF5 only re-GETs

3 points to say:

  1. @RestController returns data; @Controller returns a view name (Thymeleaf, JSP).
  2. redirect:: 2 requests, URL changes; forward:: 1 internal request, URL stays.
  3. PRG stops form resubmission on F5; a REST API returns 201 Created + Location, not a redirect.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.