diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java index 135f6f3eb2c..b7e66b91a0c 100644 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java +++ b/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/RedirectView.java @@ -45,8 +45,9 @@ import org.springframework.web.util.UriUtils; * available in the model. * *
A URL for this view is supposed to be a HTTP redirect which does the redirect via - * sending an {@link HttpStatus#SEE_OTHER} code. If HTTP 1.0 compatibility is needed, - * {@link HttpStatus#FOUND} code can be set via {@link #setStatusCode(HttpStatus)}. + * sending a {@literal 3xx} status code. By default {@link HttpStatus#SEE_OTHER} is used. + * If HTTP 1.0 compatibility is needed, {@link HttpStatus#FOUND} code can be set via + * {@link #setStatusCode(HttpStatus)}. * *
Note that the default value for the "contextRelative" flag is true. * With the flag on, URLs starting with "/" are considered relative to the web application @@ -54,7 +55,6 @@ import org.springframework.web.util.UriUtils; * root. * * @author Sebastien Deleuze - * @see #setContextRelative * @since 5.0 */ public class RedirectView extends AbstractUrlBasedView { @@ -66,49 +66,70 @@ public class RedirectView extends AbstractUrlBasedView { private HttpStatus statusCode = HttpStatus.SEE_OTHER; - private boolean propagateQueryParams = false; + private boolean propagateQuery = false; private String[] hosts; /** - * Create a new {@code RedirectView} with the given redirect URL. - * - * @see #builder(String) + * Constructor for use as a bean. + */ + public RedirectView() { + } + + /** + * Create a new {@code RedirectView} with the given URL and the {@link HttpStatus#SEE_OTHER} + * status code which is the correct code for HTTP 1.1 clients. */ public RedirectView(String redirectUrl) { super(redirectUrl); } - /** - * Return a builder for a {@code RedirectView}. + * Create a new {@code RedirectView} with the given redirect URL ans status code. Most + * frequently used ones are: + *
Default is "true": the context path will be - * prepended to the URL in such a case. If "false", an URL that starts - * with a slash will be interpreted as absolute, i.e. taken as-is. + * as relative to the current context path ({@code true}, the default) or to + * the web server root ({@code false}). */ public void setContextRelative(boolean contextRelative) { this.contextRelative = contextRelative; } /** - * Set a customized redirect status code to be used for a redirect. Default is - * {@link HttpStatus#SEE_OTHER} which is the correct code for HTTP 1.1 - * clients. This setter can be used to configure {@link HttpStatus#FOUND} - * if HTTP 1.0 clients need to be supported, or any other {@literal 3xx} - * status code. + * Return whether to interpret a given URL that starts with a slash ("/") + * as relative to the current context path ("true") or to the web server + * root ("false"). + * @return + */ + public boolean isContextRelative() { + return contextRelative; + } + + /** + * Set the redirect status code. Most frequently used ones are: + *
Defaults to {@code false}.
+ * Set whether to append the query string of the current URL to the redirected URL
+ * ({@code true}) or not ({@code false}, the default).
*/
- public void setPropagateQueryParams(boolean propagateQueryParams) {
- this.propagateQueryParams = propagateQueryParams;
+ public void setPropagateQuery(boolean propagateQuery) {
+ this.propagateQuery = propagateQuery;
+ }
+
+ /**
+ * Return whether the query string of the current URL is appended to the redirected URL
+ * ({@code true}) or not ({@code false}).
+ */
+ public boolean isPropagateQuery() {
+ return propagateQuery;
}
/**
@@ -141,6 +169,13 @@ public class RedirectView extends AbstractUrlBasedView {
this.hosts = hosts;
}
+ /**
+ * Return the configured application hosts.
+ */
+ public String[] getHosts() {
+ return this.hosts;
+ }
+
/**
* Convert model to request parameters and redirect to the given URL.
* @see #sendRedirect
@@ -173,7 +208,7 @@ public class RedirectView extends AbstractUrlBasedView {
Map Note: This class does not support localized resolution, i.e. resolving
* a symbolic view name to different resources depending on the current locale.
* @author Rossen Stoyanchev
+ * @author Sebastien Deleuze
* @since 5.0
*/
public class UrlBasedViewResolver extends ViewResolverSupport implements ViewResolver, InitializingBean {
diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/RedirectViewTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/RedirectViewTests.java
index 7d4fa194fd3..fb6fb210caf 100644
--- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/RedirectViewTests.java
+++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/RedirectViewTests.java
@@ -76,13 +76,11 @@ public class RedirectViewTests {
@Test
public void customStatusCode() {
- RedirectView view = RedirectView
- .builder("http://url.somewhere.com")
- .statusCode(HttpStatus.FOUND)
- .build();
+ String url = "http://url.somewhere.com";
+ RedirectView view = new RedirectView(url, HttpStatus.FOUND);
view.render(new HashMap<>(), MediaType.TEXT_HTML, exchange);
assertEquals(HttpStatus.FOUND, response.getStatusCode());
- assertEquals(URI.create("http://url.somewhere.com"), response.getHeaders().getLocation());
+ assertEquals(URI.create(url), response.getHeaders().getLocation());
}
@Test
@@ -137,10 +135,8 @@ public class RedirectViewTests {
@Test
public void propagateQueryParams() throws Exception {
- RedirectView view = RedirectView
- .builder("http://url.somewhere.com?foo=bar#bazz")
- .propagateQueryParams(true)
- .build();
+ RedirectView view = new RedirectView("http://url.somewhere.com?foo=bar#bazz");
+ view.setPropagateQuery(true);
request.setUri(URI.create("http://url.somewhere.com?a=b&c=d"));
view.render(new HashMap<>(), MediaType.TEXT_HTML, exchange);
assertEquals(HttpStatus.SEE_OTHER, response.getStatusCode());
diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/UrlBasedViewResolverTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/UrlBasedViewResolverTests.java
index 2f5cad661c7..c06afb6580e 100644
--- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/UrlBasedViewResolverTests.java
+++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/UrlBasedViewResolverTests.java
@@ -77,7 +77,7 @@ public class UrlBasedViewResolverTests {
@Test
public void customizedRedirectView() throws Exception {
- resolver.setRedirectViewProvider(url -> RedirectView.builder(url).statusCode(HttpStatus.FOUND).build());
+ resolver.setRedirectViewProvider(url -> new RedirectView(url, HttpStatus.FOUND));
Mono