mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
Extract code snippets from exceceptionhandlers.adoc
See gh-36175
This commit is contained in:
@@ -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
@@ -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
@@ -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[]
|
||||
Reference in New Issue
Block a user