RedirectView is the Spring MVC view class that sends an HTTP redirect to the browser instead of rendering a page. Returning it from a controller makes Spring set a redirect status and a Location header, so the client issues a new request to the target URL. It is the building block behind the redirect: prefix and the Post/Redirect/Get pattern that prevents duplicate form submissions.
It lives in org.springframework.web.servlet.view and extends AbstractUrlBasedView. A reactive twin with the same job exists for WebFlux in org.springframework.web.reactive.result.view.
A normal view renders a body. RedirectView renders nothing; it calls the servlet response to send a redirect. The steps are:
RedirectView (or a view name starting with redirect:).Location header and commits the response.GET request to that URL.By default the status is 302 Found. If you call setHttp10Compatible(false), Spring uses 303 See Other, which is the status that explicitly means "fetch the result with GET". You can also set any redirect status directly with setStatusCode.
The browser receives something like this:
Note /app: when the URL starts with / and contextRelative is true (the default), Spring prepends the application's context path. Pass true to the second constructor argument, new RedirectView(url, true), to make this explicit, or false to treat the URL as server-root-relative.
Most code does not instantiate the class. When a handler returns a view name that starts with redirect:, the UrlBasedViewResolver creates a RedirectView for you:
Prefer this form for the common case. Use an explicit RedirectView when you need to configure it, for example to set a status code or turn off model-to-query-string exposure.
A redirect starts a new request, so request attributes and model values do not survive. RedirectAttributes gives you two options:
| Method | Where the value goes | Lifetime | Use for |
|---|---|---|---|
addAttribute | Query string or URI template variable | In the URL | Simple values such as ids and filters |
addFlashAttribute | Temporary storage (session by default) | Next request only | Messages and objects you do not want in the URL |
Without RedirectAttributes, Spring MVC exposes simple-typed model attributes as query parameters on redirects, which can leak values into the URL and logs. Declaring a RedirectAttributes parameter turns that default behaviour off for that handler. On a standalone RedirectView you control it with setExposeModelAttributes(false).
You can also use URI template variables, which Spring expands from the model and the current request:
| Status | Name | Method on follow-up | Use when |
|---|---|---|---|
| 301 | Moved Permanently | Usually GET | A URL moved for good (SEO signal) |
| 302 | Found | Usually GET | Default, temporary redirect |
| 303 | See Other | Always GET | After POST, the clearest choice |
| 307 | Temporary Redirect | Same method and body | Temporary move keeping POST |
| 308 | Permanent Redirect | Same method and body | Permanent move keeping POST |
For permanent moves of public pages, use 301 or 308 so search engines transfer ranking to the new address. Importing org.springframework.http.HttpStatus is required for the snippet above.
Never build the redirect target directly from request input such as a next parameter. That is an open redirect: an attacker can send users to a malicious site from a trusted domain. Accept only relative paths you recognize, or compare against an allow-list of hosts before redirecting.
You can verify a redirect with MockMvc:
For REST APIs returning JSON, a view-based redirect is rarely right. Return a ResponseEntity with the Location header and a 201 or 3xx status instead:
A forward (forward:/path) happens inside the server in the same request, and the browser URL does not change. A redirect sends a response telling the browser to make a new request, so the URL changes and request data is lost.
By default it sends 302. Use setStatusCode to send 301, 303, 307 or 308, or setHttp10Compatible(false) to get 303.
Spring exposes simple model attributes as query parameters on redirects. Use RedirectAttributes or call setExposeModelAttributes(false) to stop that.
Use addFlashAttribute on RedirectAttributes. The value is available in the next request's model and then discarded.
RedirectView turns a controller result into an HTTP redirect with a status code and Location header. Use the redirect: prefix for everyday cases, RedirectAttributes to pass data safely, and an explicit status such as 303 or 301 when the semantics matter. Validate any user-supplied target to avoid open redirects, and return a ResponseEntity with Location in REST controllers.