Skip to content

Commit 538afc4

Browse files
committed
Make some methods in BasicErrorController protected
Spring MVC requires all handlers for the same path to be on the same handler so if anyone wants to add new handlers for different content types they have to copy a lot of code from BasicErrorController. This change increases the visibility of the basic utility methods in BasicErrorController so that custom handlers can be added easily. Fixes gh-3828
1 parent a0870c1 commit 538afc4

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* @see ErrorAttributes
4444
*/
4545
@Controller
46+
@RequestMapping("${error.path:/error}")
4647
public class BasicErrorController implements ErrorController {
4748

4849
@Value("${error.path:/error}")
@@ -60,15 +61,15 @@ public String getErrorPath() {
6061
return this.errorPath;
6162
}
6263

63-
@RequestMapping(value = "${error.path:/error}", produces = "text/html")
64+
@RequestMapping(produces = "text/html")
6465
public ModelAndView errorHtml(HttpServletRequest request) {
6566
return new ModelAndView("error", getErrorAttributes(request, false));
6667
}
6768

68-
@RequestMapping(value = "${error.path:/error}")
69+
@RequestMapping
6970
@ResponseBody
7071
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
71-
Map<String, Object> body = getErrorAttributes(request, getTraceParameter(request));
72+
Map<String, Object> body = getErrorAttributes(request);
7273
HttpStatus status = getStatus(request);
7374
return new ResponseEntity<Map<String, Object>>(body, status);
7475
}
@@ -81,14 +82,18 @@ private boolean getTraceParameter(HttpServletRequest request) {
8182
return !"false".equals(parameter.toLowerCase());
8283
}
8384

84-
private Map<String, Object> getErrorAttributes(HttpServletRequest request,
85+
protected Map<String, Object> getErrorAttributes(HttpServletRequest request) {
86+
return getErrorAttributes(request, getTraceParameter(request));
87+
}
88+
89+
protected Map<String, Object> getErrorAttributes(HttpServletRequest request,
8590
boolean includeStackTrace) {
8691
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
8792
return this.errorAttributes.getErrorAttributes(requestAttributes,
8893
includeStackTrace);
8994
}
9095

91-
private HttpStatus getStatus(HttpServletRequest request) {
96+
protected HttpStatus getStatus(HttpServletRequest request) {
9297
Integer statusCode = (Integer) request
9398
.getAttribute("javax.servlet.error.status_code");
9499
if (statusCode != null) {

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

+6
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,12 @@ the same data in HTML format (to customize it just add a `View` that resolves to
14011401
`ErrorController` and register a bean definition of that type, or simply add a bean of
14021402
type `ErrorAttributes` to use the existing mechanism but replace the contents.
14031403

1404+
TIP: The `BasicErrorController` can be used as a base class for a custom `ErrorController`.
1405+
This is particularly useful if you want to add a handler for a new content type (the default
1406+
is to handle `text/html` specifically and provide a fallback for everything else). To do that
1407+
just extend `BasicErrorController` and add a public method with a `@RequestMapping` that
1408+
has a `produces` attribute, and create a bean of your new type.
1409+
14041410
If you want more specific error pages for some conditions, the embedded servlet containers
14051411
support a uniform Java DSL for customizing the error handling. Assuming that you have a
14061412
mapping for `/400`:

0 commit comments

Comments
 (0)