Skip to content

Commit 9a4df5a

Browse files
committed
DefaultHandlerExceptionResolver respects custom ModelAndView
Closes gh-29971
1 parent f54e1ef commit 9a4df5a

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,8 @@ else if (ex instanceof AsyncRequestTimeoutException) {
212212
(AsyncRequestTimeoutException) ex, request, response, handler);
213213
}
214214

215-
if (mav == null) {
216-
return handleErrorResponse((ErrorResponse) ex, request, response, handler);
217-
}
215+
return (mav != null ? mav :
216+
handleErrorResponse((ErrorResponse) ex, request, response, handler));
218217
}
219218

220219
// Other, lower level exceptions

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolverTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@
2020
import java.util.Arrays;
2121
import java.util.Collections;
2222

23+
import jakarta.servlet.http.HttpServletRequest;
24+
import jakarta.servlet.http.HttpServletResponse;
2325
import org.junit.jupiter.api.BeforeEach;
2426
import org.junit.jupiter.api.Test;
2527

@@ -32,6 +34,7 @@
3234
import org.springframework.http.converter.HttpMessageNotReadableException;
3335
import org.springframework.http.converter.HttpMessageNotWritableException;
3436
import org.springframework.http.server.ServletServerHttpRequest;
37+
import org.springframework.lang.Nullable;
3538
import org.springframework.validation.BeanPropertyBindingResult;
3639
import org.springframework.validation.BindException;
3740
import org.springframework.web.HttpMediaTypeNotSupportedException;
@@ -42,6 +45,7 @@
4245
import org.springframework.web.bind.ServletRequestBindingException;
4346
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
4447
import org.springframework.web.multipart.support.MissingServletRequestPartException;
48+
import org.springframework.web.servlet.HandlerExceptionResolver;
4549
import org.springframework.web.servlet.ModelAndView;
4650
import org.springframework.web.servlet.NoHandlerFoundException;
4751
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
@@ -232,6 +236,27 @@ public void handleAsyncRequestTimeoutException() throws Exception {
232236
assertThat(response.getStatus()).as("Invalid status code").isEqualTo(503);
233237
}
234238

239+
@Test
240+
public void customModelAndView() {
241+
ModelAndView expected = new ModelAndView();
242+
243+
HandlerExceptionResolver resolver = new DefaultHandlerExceptionResolver() {
244+
245+
@Override
246+
protected ModelAndView handleHttpRequestMethodNotSupported(
247+
HttpRequestMethodNotSupportedException ex, HttpServletRequest request,
248+
HttpServletResponse response, @Nullable Object handler) {
249+
250+
return expected;
251+
}
252+
};
253+
254+
HttpRequestMethodNotSupportedException ex = new HttpRequestMethodNotSupportedException("GET");
255+
256+
ModelAndView actual = resolver.resolveException(request, response, null, ex);
257+
assertThat(actual).isSameAs(expected);
258+
}
259+
235260

236261
@SuppressWarnings("unused")
237262
public void handle(String arg) {

0 commit comments

Comments
 (0)