Browse Source

Extract code snippets from exceceptionhandlers.adoc

See gh-36175
pull/36178/head
Sébastien Deleuze 2 weeks ago
parent
commit
49da766551
  1. 37
      framework-docs/modules/ROOT/pages/web/webmvc/mvc-servlet/exceptionhandlers.adoc
  2. 38
      framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvcanncustomerservletcontainererrorpage/ErrorController.java
  3. 35
      framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvcanncustomerservletcontainererrorpage/ErrorController.kt

37
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 @@ -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<String, Object> handle(HttpServletRequest request) {
Map<String, Object> 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<String, Any> {
val map = HashMap<String, Any>()
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`.

38
framework-docs/src/main/java/org/springframework/docs/web/webmvc/mvcservlet/mvcanncustomerservletcontainererrorpage/ErrorController.java

@ -0,0 +1,38 @@ @@ -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<String, Object> handle(HttpServletRequest request) {
Map<String, Object> 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[]

35
framework-docs/src/main/kotlin/org/springframework/docs/web/webmvc/mvcservlet/mvcanncustomerservletcontainererrorpage/ErrorController.kt

@ -0,0 +1,35 @@ @@ -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<String, Any> {
val map = HashMap<String, Any>()
map["status"] = request.getAttribute("jakarta.servlet.error.status_code")!!
map["reason"] = request.getAttribute("jakarta.servlet.error.message")!!
return map
}
}
// end::snippet[]
Loading…
Cancel
Save