In the last tutorial we saw how to use ViewControllerRegistry during configuration time to map a url directly to a view. Here we are going to understand another feature of ViewControllerRegistry, that is, how to redirect one URL to another URL directly without using a @Controller.
Note that Spring also supports URL redirection from @Controller method by using RedirectView or by returning 'redirect:' prefix. If not already familiar with URL redirection, we suggest to check out Basics of URL redirection and URL redirection in Java Servlet tutorials.
Example
The configuration class
@SpringBootApplication
public class Main extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers (ViewControllerRegistry registry) {
RedirectViewControllerRegistration r =
registry.addRedirectViewController("/test", "/test2");
}
public static void main (String[] args) {
SpringApplication.run(Main.class, args);
}
}
In above configuration, we are redirecting '/test' to '/test2'. To handle /test2, we are going to map it to a controller method.
The Controller handling /test2
@Controller
public class MyController {
@RequestMapping("/test2")
@ResponseBody
private String handleRequest () {
return "response from controller";
}
}
Spring Boot properties
src/main/resources/application.propertiesspring.mvc.view.prefix= /WEB-INF/pages/
spring.mvc.view.suffix= .jsp
server.contextPath= /example
Running web application
mvn spring-boot:run
Output
Enter URL http://localhost:8080/test:
In chrome browser, Developer tools > Network tab:
Changing default status code of 302
As seen in above output, the response for '/test' returns status code 302 along with Location header specifying new redirect URL. We can change the default 302 status code by using method RedirectViewControllerRegistration #setStatusCode(..) :
@Override
public void addViewControllers (ViewControllerRegistry registry) {
RedirectViewControllerRegistration r =
registry.addRedirectViewController("/test", "/test2");
r.setStatusCode(HttpStatus.SEE_OTHER);
}
Above code will set the status code to 303 (See Other). If the status code is not of the standard redirect codes (3xx), an exception will be thrown during startup time:
Caused by: java.lang.IllegalArgumentException: Not a redirect status code.
at org.springframework.util.Assert.isTrue(Assert.java:68)
at org.springframework.web.servlet.config.annotation.RedirectViewControllerRegistration.setStatusCode(RedirectViewControllerRegistration.java:57)
.......
Resolving URL relative to root context
The flag set by RedirectViewControllerRegistration #setContextRelative(boolean) is used to resolve the provided redirect URI. If true, all URI will be resolved relative to the current ServletContext. By default it's set to true. Note that similar flag is set with RedirectView instance, but there it's false by default. We would probably never want to set this flag to false, because resolving URI relative to application context root is a very convenient way to go.
Passing query parameters to the redirected resource
By using the method RedirectViewControllerRegistration #setKeepQueryParams(boolean) , we can specify whether the query parameters should be repeated with the redirected destination. By default it's false that means if we send query parameters with the original URI '/test' (e.g. '/test?param=paramValue'), they won't be carried over to the destination URI '/test2' unless we set this flag to true.
Example ProjectDependencies and Technologies Used: - Spring Boot 1.4.4.RELEASE
Corresponding Spring Version 4.3.6.RELEASE - spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring
MVC. Uses Tomcat as the default embedded container.
- tomcat-embed-jasper 8.5.11: Core Tomcat implementation.
- JDK 1.8
- Maven 3.3.9
|