Skip to content

Commit 089d938

Browse files
committed
Set throwExceptionIfNoHandlerFound=true and deprecate
Closes gh-29491
1 parent c00508d commit 089d938

File tree

7 files changed

+23
-12
lines changed

7 files changed

+23
-12
lines changed

framework-docs/modules/ROOT/pages/web/webmvc/mvc-ann-rest-exceptions.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ Spring MVC exceptions:
162162
| (default)
163163
|
164164

165+
| `NoResourceFoundException`
166+
| (default)
167+
|
168+
165169
| `TypeMismatchException`
166170
| (default)
167171
| `+{0}+` property name, `+{1}+` property value

framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/sequence.adoc

+1-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ initialization parameters (`init-param` elements) to the Servlet declaration in
6262
The exception can then be caught with a `HandlerExceptionResolver` (for example, by using an
6363
`@ExceptionHandler` controller method) and handled as any others.
6464

65-
By default, this is set to `false`, in which case the `DispatcherServlet` sets the
66-
response status to 404 (NOT_FOUND) without raising an exception.
65+
As of 6.1, this property is set to `true` and deprecated.
6766

6867
Note that, if xref:web/webmvc/mvc-config/default-servlet-handler.adoc[default servlet handling] is
6968
also configured, unresolved requests are always forwarded to the default servlet

spring-test/src/test/java/org/springframework/test/web/servlet/samples/client/standalone/ExceptionHandlerTests.java

+1-2
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.
@@ -156,7 +156,6 @@ void globalRestPersonControllerExceptionHandlerTakesPrecedenceOverGlobalExceptio
156156
void noHandlerFound() {
157157
WebTestClient client = MockMvcWebTestClient.bindToController(new RestPersonController())
158158
.controllerAdvice(RestGlobalExceptionHandler.class, RestPersonControllerExceptionHandler.class)
159-
.dispatcherServletCustomizer(servlet -> servlet.setThrowExceptionIfNoHandlerFound(true))
160159
.build();
161160

162161
client.get().uri("/bogus")

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ExceptionHandlerTests.java

+1-2
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.
@@ -138,7 +138,6 @@ void globalRestPersonControllerExceptionHandlerTakesPrecedenceOverGlobalExceptio
138138
void noHandlerFound() throws Exception {
139139
standaloneSetup(RestPersonController.class)
140140
.setControllerAdvice(RestGlobalExceptionHandler.class, RestPersonControllerExceptionHandler.class)
141-
.addDispatcherServletCustomizer(servlet -> servlet.setThrowExceptionIfNoHandlerFound(true))
142141
.build()
143142
.perform(get("/bogus").accept(MediaType.APPLICATION_JSON))
144143
.andExpect(status().isOk())

spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public class DispatcherServlet extends FrameworkServlet {
305305
private boolean detectAllViewResolvers = true;
306306

307307
/** Throw a NoHandlerFoundException if no Handler was found to process this request? *.*/
308-
private boolean throwExceptionIfNoHandlerFound = false;
308+
private boolean throwExceptionIfNoHandlerFound = true;
309309

310310
/** Perform cleanup of request attributes after include request?. */
311311
private boolean cleanupAfterInclude = true;
@@ -467,7 +467,12 @@ public void setDetectAllViewResolvers(boolean detectAllViewResolvers) {
467467
* <p>Default is "false", meaning the DispatcherServlet sends a NOT_FOUND error through the
468468
* Servlet response.
469469
* @since 4.0
470+
* @deprecated as of 6.1 this property is set to {@code true} by default, and
471+
* should not need to be customized; in effect, {@link DispatcherServlet}
472+
* should always raise {@link NoHandlerFoundException} and allow it to be
473+
* handled through a {@link HandlerExceptionResolver}.
470474
*/
475+
@Deprecated(since = "6.1", forRemoval = true)
471476
public void setThrowExceptionIfNoHandlerFound(boolean throwExceptionIfNoHandlerFound) {
472477
this.throwExceptionIfNoHandlerFound = throwExceptionIfNoHandlerFound;
473478
}

spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,26 @@ public void refresh() throws BeansException {
150150
registerSingleton("myServlet", MyServlet.class);
151151

152152
pvs = new MutablePropertyValues();
153-
pvs.add("order", "1");
153+
pvs.add("order", "2");
154154
pvs.add("exceptionMappings",
155155
"java.lang.IllegalAccessException=failed2\n" +
156156
"ServletRequestBindingException=failed3");
157157
pvs.add("defaultErrorView", "failed0");
158-
registerSingleton("exceptionResolver1", SimpleMappingExceptionResolver.class, pvs);
158+
registerSingleton("exceptionResolver2", SimpleMappingExceptionResolver.class, pvs);
159159

160160
pvs = new MutablePropertyValues();
161-
pvs.add("order", "0");
161+
pvs.add("order", "1");
162162
pvs.add("exceptionMappings", "java.lang.Exception=failed1");
163163
pvs.add("mappedHandlers", ManagedList.of(new RuntimeBeanReference("anotherLocaleHandler")));
164164
pvs.add("defaultStatusCode", "500");
165165
pvs.add("defaultErrorView", "failed2");
166-
registerSingleton("handlerExceptionResolver", SimpleMappingExceptionResolver.class, pvs);
166+
registerSingleton("exceptionResolver1", SimpleMappingExceptionResolver.class, pvs);
167+
168+
pvs = new MutablePropertyValues();
169+
pvs.add("order", "0");
170+
pvs.add("exceptionMappings", "org.springframework.web.servlet.NoHandlerFoundException=notFound");
171+
pvs.add("defaultStatusCode", "404");
172+
registerSingleton("exceptionResolver0", SimpleMappingExceptionResolver.class, pvs);
167173

168174
registerSingleton("multipartResolver", MockMultipartResolver.class);
169175
registerSingleton("testListener", TestApplicationListener.class);

spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,6 @@ public void throwExceptionIfNoHandlerFound() throws ServletException, IOExceptio
591591
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
592592
complexDispatcherServlet.setContextClass(SimpleWebApplicationContext.class);
593593
complexDispatcherServlet.setNamespace("test");
594-
complexDispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
595594
complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
596595

597596
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown");

0 commit comments

Comments
 (0)