From 49da7665518936624d3825dec8c36b27c198e378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Sat, 17 Jan 2026 18:04:23 +0100 Subject: [PATCH] Extract code snippets from exceceptionhandlers.adoc See gh-36175 --- .../webmvc/mvc-servlet/exceptionhandlers.adoc | 37 +----------------- .../ErrorController.java | 38 +++++++++++++++++++ .../ErrorController.kt | 35 +++++++++++++++++ 3 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvcanncustomerservletcontainererrorpage/ErrorController.java create mode 100644 framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvcanncustomerservletcontainererrorpage/ErrorController.kt diff --git a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/exceptionhandlers.adoc b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/exceptionhandlers.adoc index 21e062296de..3a0d285b02e 100644 --- a/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/exceptionhandlers.adoc +++ b/framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/exceptionhandlers.adoc @@ -74,42 +74,7 @@ Servlet container makes an ERROR dispatch within the container to the configured to a `@Controller`, which could be implemented to return an error view name with a model or to render a JSON response, as the following example shows: -[tabs] -====== -Java:: -+ -[source,java,indent=0,subs="verbatim,quotes"] ----- - @RestController - public class ErrorController { - - @RequestMapping(path = "/error") - public Map handle(HttpServletRequest request) { - Map map = new HashMap<>(); - map.put("status", request.getAttribute("jakarta.servlet.error.status_code")); - map.put("reason", request.getAttribute("jakarta.servlet.error.message")); - return map; - } - } ----- - -Kotlin:: -+ -[source,kotlin,indent=0,subs="verbatim,quotes"] ----- - @RestController - class ErrorController { - - @RequestMapping(path = ["/error"]) - fun handle(request: HttpServletRequest): Map { - val map = HashMap() - map["status"] = request.getAttribute("jakarta.servlet.error.status_code") - map["reason"] = request.getAttribute("jakarta.servlet.error.message") - return map - } - } ----- -====== +include-code::./ErrorController[tag=snippet,indent=0] TIP: The Servlet API does not provide a way to create error page mappings in Java. You can, however, use both a `WebApplicationInitializer` and a minimal `web.xml`. diff --git a/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvcanncustomerservletcontainererrorpage/ErrorController.java b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvcanncustomerservletcontainererrorpage/ErrorController.java new file mode 100644 index 00000000000..4c68fd246fb --- /dev/null +++ b/framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvcanncustomerservletcontainererrorpage/ErrorController.java @@ -0,0 +1,38 @@ +/* + * Copyright 2002-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.web.webmvc.mvcservlet.mvcanncustomerservletcontainererrorpage; + +import java.util.HashMap; +import java.util.Map; + +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +// tag::snippet[] +@RestController +public class ErrorController { + + @RequestMapping(path = "/error") + public Map handle(HttpServletRequest request) { + Map map = new HashMap<>(); + map.put("status", request.getAttribute("jakarta.servlet.error.status_code")); + map.put("reason", request.getAttribute("jakarta.servlet.error.message")); + return map; + } +} +// end::snippet[] diff --git a/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvcanncustomerservletcontainererrorpage/ErrorController.kt b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvcanncustomerservletcontainererrorpage/ErrorController.kt new file mode 100644 index 00000000000..67123f67509 --- /dev/null +++ b/framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvcanncustomerservletcontainererrorpage/ErrorController.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2002-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.docs.web.webmvc.mvcservlet.mvcanncustomerservletcontainererrorpage + +import jakarta.servlet.http.HttpServletRequest +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +// tag::snippet[] +@RestController +class ErrorController { + + @RequestMapping(path = ["/error"]) + fun handle(request: HttpServletRequest): Map { + val map = HashMap() + map["status"] = request.getAttribute("jakarta.servlet.error.status_code")!! + map["reason"] = request.getAttribute("jakarta.servlet.error.message")!! + return map + } +} +// end::snippet[]