From fe944ca5cdc5bd7d626b4ec023d1c9da9cf9bf0f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 14 Feb 2023 21:04:56 +0000 Subject: [PATCH] Polish "Document addition config for throw-exception-if-no-handler-found" See gh-31660 --- .../spring-boot-docs/src/docs/asciidoc/web/servlet.adoc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/servlet.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/servlet.adoc index 755d357f1b..925a8c57ac 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/servlet.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/web/servlet.adoc @@ -112,8 +112,6 @@ Any resources with a path in `+/webjars/**+` are served from jar files if they a TIP: Do not use the `src/main/webapp` directory if your application is packaged as a jar. Although this directory is a common standard, it works *only* with war packaging, and it is silently ignored by most build tools if you generate a jar. -TIP: If you configure `spring.mvc.throw-exception-if-no-handler-found=true`, it will not work with default `+spring.mvc.static-path-pattern=/**+`, because each url matches `+/**+`. Url without handler must be excluded from _static-path-pattern_ to `NoHandlerFoundException` be thrown. Quite effective exclusion is `spring.mvc.static-path-pattern=` - Spring Boot also supports the advanced resource handling features provided by Spring MVC, allowing use cases such as cache-busting static resources or using version agnostic URLs for Webjars. To use version agnostic URLs for Webjars, add the `webjars-locator-core` dependency. @@ -278,6 +276,11 @@ and is incompatible with suffix pattern matching (configprop:spring.mvc.pathmatc configprop:spring.mvc.pathmatch.use-registered-suffix-pattern[deprecated]) or mapping the `DispatcherServlet` with a servlet prefix (configprop:spring.mvc.servlet.path[]). +By default, Spring MVC will send a 404 Not Found error response if a handler is not found for a request. +To have a `NoHandlerFoundException` thrown instead, set configprop:spring.mvc.throw-exception-if-no-handler-found to `true`. +Note that, by default, the <> is mapped to `+/**+` and will, therefore, provide a handler for all requests. +For a `NoHandlerFoundException` to be thrown, you must also set configprop:spring.mvc.static-path-pattern[] to a more specific value such as `/resources/**` or set configprop:spring.web.resources.add-mappings[] to `false` to disable serving of static content entirely. + [[web.servlet.spring-mvc.binding-initializer]]