3333import org .jspecify .annotations .Nullable ;
3434
3535import org .springframework .http .HttpHeaders ;
36- import org .springframework .http .HttpStatus ;
36+ import org .springframework .http .HttpStatusCode ;
3737import org .springframework .http .server .PathContainer ;
3838import org .springframework .http .server .RequestPath ;
39+ import org .springframework .util .Assert ;
3940import org .springframework .util .LinkedMultiValueMap ;
4041import org .springframework .util .MultiValueMap ;
4142import org .springframework .util .StringUtils ;
@@ -146,11 +147,11 @@ interface TrailingSlashSpec {
146147 /**
147148 * Handle requests by sending a redirect to the same URL but the
148149 * trailing slash trimmed.
149- * @param status the redirect status to use
150+ * @param statusCode the redirect status to use
150151 * @return the top level {@link Builder}, which allows adding more
151152 * handlers and then building the Filter instance.
152153 */
153- Builder redirect (HttpStatus status );
154+ Builder redirect (HttpStatusCode statusCode );
154155
155156 /**
156157 * Handle the request by wrapping it in order to trim the trailing
@@ -207,8 +208,8 @@ public TrailingSlashSpec intercept(Consumer<HttpServletRequest> consumer) {
207208 }
208209
209210 @ Override
210- public Builder redirect (HttpStatus status ) {
211- Handler handler = new RedirectTrailingSlashHandler (status , this .interceptor );
211+ public Builder redirect (HttpStatusCode statusCode ) {
212+ Handler handler = new RedirectTrailingSlashHandler (statusCode , this .interceptor );
212213 return DefaultBuilder .this .addHandler (this .pathPatterns , handler );
213214 }
214215
@@ -288,11 +289,13 @@ protected String trimTrailingSlash(String path) {
288289 */
289290 private static final class RedirectTrailingSlashHandler extends AbstractTrailingSlashHandler {
290291
291- private final HttpStatus httpStatus ;
292+ private final HttpStatusCode statusCode ;
292293
293- RedirectTrailingSlashHandler (HttpStatus httpStatus , @ Nullable Consumer <HttpServletRequest > interceptor ) {
294+ RedirectTrailingSlashHandler (HttpStatusCode statusCode , @ Nullable Consumer <HttpServletRequest > interceptor ) {
294295 super (interceptor );
295- this .httpStatus = httpStatus ;
296+ Assert .isTrue (statusCode .is3xxRedirection (), "HTTP status code for redirect handlers " +
297+ "must be in the Redirection class (3xx)" );
298+ this .statusCode = statusCode ;
296299 }
297300
298301 @ Override
@@ -305,7 +308,7 @@ public void handleInternal(HttpServletRequest request, HttpServletResponse respo
305308 }
306309
307310 response .resetBuffer ();
308- response .setStatus (this .httpStatus .value ());
311+ response .setStatus (this .statusCode .value ());
309312 response .setHeader (HttpHeaders .LOCATION , location );
310313 response .flushBuffer ();
311314 }
0 commit comments