Skip to content

Commit 422444c

Browse files
committed
Add throwExceptionIfNoHandlerFound property
Add a property to customize if an exception should be thrown when no handler was found to process a given request. Closes gh-4000
1 parent 535a696 commit 422444c

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
3535
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
3636
import org.springframework.boot.context.embedded.ServletRegistrationBean;
37+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3738
import org.springframework.boot.context.web.SpringBootServletInitializer;
3839
import org.springframework.context.annotation.Bean;
3940
import org.springframework.context.annotation.ConditionContext;
@@ -53,6 +54,7 @@
5354
*
5455
* @author Phillip Webb
5556
* @author Dave Syer
57+
* @author Stephane Nicoll
5658
*/
5759
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
5860
@Configuration
@@ -74,17 +76,24 @@ public class DispatcherServletAutoConfiguration {
7476
@Configuration
7577
@Conditional(DefaultDispatcherServletCondition.class)
7678
@ConditionalOnClass(ServletRegistration.class)
79+
@EnableConfigurationProperties(WebMvcProperties.class)
7780
protected static class DispatcherServletConfiguration {
7881

7982
@Autowired
8083
private ServerProperties server;
8184

85+
@Autowired
86+
private WebMvcProperties webMvcProperties;
87+
8288
@Autowired(required = false)
8389
private MultipartConfigElement multipartConfig;
8490

8591
@Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
8692
public DispatcherServlet dispatcherServlet() {
87-
return new DispatcherServlet();
93+
DispatcherServlet dispatcherServlet = new DispatcherServlet();
94+
dispatcherServlet.setThrowExceptionIfNoHandlerFound(
95+
this.webMvcProperties.isThrowExceptionIfNoHandlerFound());
96+
return dispatcherServlet;
8897
}
8998

9099
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ public class WebMvcProperties {
5353
*/
5454
private boolean ignoreDefaultModelOnRedirect = true;
5555

56+
/**
57+
* If a "NoHandlerFoundException" should be thrown if no Handler was found to process
58+
* a request.
59+
*/
60+
private boolean throwExceptionIfNoHandlerFound = false;
61+
5662
private final Async async = new Async();
5763

5864
private final View view = new View();
@@ -90,6 +96,14 @@ public void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect
9096
this.ignoreDefaultModelOnRedirect = ignoreDefaultModelOnRedirect;
9197
}
9298

99+
public boolean isThrowExceptionIfNoHandlerFound() {
100+
return this.throwExceptionIfNoHandlerFound;
101+
}
102+
103+
public void setThrowExceptionIfNoHandlerFound(boolean throwExceptionIfNoHandlerFound) {
104+
this.throwExceptionIfNoHandlerFound = throwExceptionIfNoHandlerFound;
105+
}
106+
93107
public Async getAsync() {
94108
return this.async;
95109
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfigurationTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import javax.servlet.MultipartConfigElement;
2020
import javax.servlet.http.HttpServletRequest;
2121

22+
import org.junit.After;
2223
import org.junit.Test;
24+
import org.springframework.beans.DirectFieldAccessor;
2325
import org.springframework.beans.factory.UnsatisfiedDependencyException;
2426
import org.springframework.boot.context.embedded.MultipartConfigFactory;
2527
import org.springframework.boot.context.embedded.ServletRegistrationBean;
@@ -49,6 +51,13 @@ public class DispatcherServletAutoConfigurationTests {
4951

5052
private AnnotationConfigWebApplicationContext context;
5153

54+
@After
55+
public void closeContext() {
56+
if (this.context != null) {
57+
this.context.close();
58+
}
59+
}
60+
5261
@Test
5362
public void registrationProperties() throws Exception {
5463
this.context = new AnnotationConfigWebApplicationContext();
@@ -137,6 +146,20 @@ public void renamesMultipartResolver() throws Exception {
137146
instanceOf(MockMultipartResolver.class));
138147
}
139148

149+
@Test
150+
public void dispatcherServletConfig() {
151+
this.context = new AnnotationConfigWebApplicationContext();
152+
this.context.setServletContext(new MockServletContext());
153+
this.context.register(ServerPropertiesAutoConfiguration.class,
154+
DispatcherServletAutoConfiguration.class);
155+
EnvironmentTestUtils.addEnvironment(this.context,
156+
"spring.mvc.throw-exception-if-no-handler-found:true");
157+
this.context.refresh();
158+
DispatcherServlet bean = this.context.getBean(DispatcherServlet.class);
159+
assertEquals(true, new DirectFieldAccessor(bean).
160+
getPropertyValue("throwExceptionIfNoHandlerFound"));
161+
}
162+
140163
@Configuration
141164
protected static class MultipartConfiguration {
142165

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ content into your application; rather pick only the properties that you need.
151151
spring.mvc.favicon.enabled=true
152152
spring.mvc.message-codes-resolver-format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE
153153
spring.mvc.ignore-default-model-on-redirect=true # if the content of the "default" model should be ignored redirects
154+
spring.mvc.throw-exception-if-no-handler-found=false # if a "NoHandlerFoundException" should be thrown if no Handler was found to process a request
154155
spring.mvc.async.request-timeout= # async request timeout in milliseconds
155156
spring.mvc.view.prefix= # MVC view prefix
156157
spring.mvc.view.suffix= # ... and suffix

0 commit comments

Comments
 (0)