RedirectAttributes is a sub-interface of Model.
It is a preferred way to pass attributes to redirect target.
Using Model attributes for passing query parameters (Possible before Spring 7) is not always desirable as it may conflict some attributes used for rendering purposes.
The following example demonstrates the use of RedirectAttributes
Disable default usage of Model attributes during redirection (Pre Spring 6)
The RequestMappingHandlerAdapter provides a flag called "ignoreDefaultModelOnRedirect" (deprecated in Spring 6 and removed in Spring 7) that can be used to indicate the content of the default Model should never be used if a controller method redirects. Pre Spring 6 this flag was set to false by default, so if we want to use RedirectAttributes with a version before Spring 6, we have to disable model attributes mapping to redirect query parameter by setting RequestMappingHandlerAdapter#ignoreDefaultModelOnRedirect flag to true in our @Configuration class:
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig {
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
RequestMappingHandlerAdapter adapter =
new RequestMappingHandlerAdapter();
adapter.setIgnoreDefaultModelOnRedirect(true);
return adapter;
}
}
The Controller
package com.logicbig.example;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;
@Controller
public class MyController {
@RequestMapping(value = "test")
public String handleTestRequest(RedirectAttributes ra) {
ra.addAttribute("attr", "attrVal");
ra.addFlashAttribute("flashAttr", "flashAttrVal");
RedirectView rv = new RedirectView();
rv.setUrl("/test2");
return "redirect:/test2";
}
@RequestMapping("test2")
@ResponseBody
public String handleTest2Request(@RequestParam("attr") String attr,
@ModelAttribute("flashAttr") String flashAttr) {
return String.format("test2 response attr: %s, flashAttr: %s",
attr, flashAttr);
}
}
As seen above, RedirectAttributes also provides a way to add flash attributes.
What is Flash Attribute?
Flash Attributes provide a way for one request to store attributes intended to used in another controller method during URL redirection.
The flash attribute data is not sent to the client browser as a part of 'Location' URL header.
It is saved on the server side temporarily in the HTTP Session before redirection happens and is made available in the target handler method after the redirect and then removed immediately.
We can use flash attribute as any object type and can access it in the target handler method using @ModelAttribute
Running The Example
To try examples, run embedded Jetty (configured in pom.xml of example project below):
mvn jetty:run
Enter url http://localhost:8080/test in your browser:
Before entering the above URL in Chrome, we pressed F12 and click on Network tab at the top of the panel. This is to inspect the redirect request (2 round trips).
Using curl
$ curl -s -iL "http://localhost:8080/test" HTTP/1.1 302 Found Date: Tue, 04 Aug 2026 09:57:03 GMT Content-Language: en-US Set-Cookie: JSESSIONID=node0t742db2dpnq91feg56olodmbm2.node0; Path=/ Expires: Thu, 01 Jan 1970 00:00:00 GMT Location: http://localhost:8080/test2;jsessionid=node0t742db2dpnq91feg56olodmbm2.node0?attr=attrVal Content-Length: 0 Server: Jetty(9.4.53.v20231009)
HTTP/1.1 200 OK Date: Tue, 04 Aug 2026 09:57:03 GMT Content-Type: text/plain;charset=iso-8859-1 Content-Length: 53 Server: Jetty(9.4.53.v20231009)
test2 response attr: attrVal, flashAttr: flashAttrVal
Integration Test
package com.logicbig.example;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.assertj.MockMvcTester;
import org.springframework.web.context.WebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitWebConfig(MyWebConfig.class)
public class ControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvcTester mockMvcTester;
@BeforeEach
public void setup() {
this.mockMvcTester = MockMvcTester.from(this.wac);
}
@Test
public void testController() {
MockHttpSession session = new MockHttpSession();
String redirectedUrl =
this.mockMvcTester.get()
.uri("/test")
.session(session)
.exchange()
.getResponse()
.getRedirectedUrl();
assertThat(redirectedUrl).isEqualTo("/test2?attr=attrVal");
assertThat(this.mockMvcTester.get().uri(redirectedUrl)
.session(session)
.exchange())
.hasStatusOk()
.bodyText().isEqualTo("test2 response "
+ "attr: attrVal, "
+ "flashAttr: flashAttrVal");
}
}
mvn clean test -Dtest="ControllerTest" Output$ mvn clean test -Dtest="ControllerTest" [INFO] Scanning for projects... [INFO] [INFO] ----------< com.logicbig.example:spring-redirect-attributes >----------- [INFO] Building spring-redirect-attributes 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- clean:3.2.0:clean (default-clean) @ spring-redirect-attributes --- [INFO] Deleting D:\example-projects\spring-mvc\spring-redirect-attributes\target [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ spring-redirect-attributes --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\spring-mvc\spring-redirect-attributes\src\main\resources [INFO] [INFO] --- compiler:3.15.0:compile (default-compile) @ spring-redirect-attributes --- [INFO] Recompiling the module because of changed source code. [INFO] Compiling 3 source files with javac [debug target 25] to target\classes [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ spring-redirect-attributes --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\spring-mvc\spring-redirect-attributes\src\test\resources [INFO] [INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ spring-redirect-attributes --- [INFO] Recompiling the module because of changed dependency. [INFO] Compiling 2 source files with javac [debug target 25] to target\test-classes [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ spring-redirect-attributes --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.ControllerTest [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.575 s -- in com.logicbig.example.ControllerTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 9.808 s [INFO] Finished at: 2026-08-04T21:42:15+08:00 [INFO] ------------------------------------------------------------------------ INFO: Completed initialization in 2 ms
Example ProjectDependencies and Technologies Used: - spring-webmvc 7.0.6 (Spring Web MVC)
Version Compatibility: 3.2.9.RELEASE - 7.0.6 Version compatibilities of spring-webmvc with this example: Versions in green have been tested.
- jakarta.servlet-api 6.1.0 (Jakarta Servlet API documentation)
- spring-test 7.0.6 (Spring TestContext Framework)
- junit-jupiter-engine 6.0.3 (Module "junit-jupiter-engine" of JUnit)
- hamcrest 3.0 (Core API and libraries of hamcrest matcher framework)
- assertj-core 3.26.3 (Rich and fluent assertions for testing in Java)
- JDK 25
- Maven 3.9.11
|