From 8f91e48a32f64324aca7049d1069f0e2c64043a2 Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Mon, 15 Feb 2021 17:57:21 -0300 Subject: [PATCH 1/2] added include-message property (commented out as the other properties), and updated deprecated getErrorAttributes method (caused not including the message property) --- .../com/baeldung/web/config/MyCustomErrorAttributes.java | 5 +++-- spring-boot-rest/src/main/resources/application.properties | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/spring-boot-rest/src/main/java/com/baeldung/web/config/MyCustomErrorAttributes.java b/spring-boot-rest/src/main/java/com/baeldung/web/config/MyCustomErrorAttributes.java index 1948d5552f..5e776c0e29 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/web/config/MyCustomErrorAttributes.java +++ b/spring-boot-rest/src/main/java/com/baeldung/web/config/MyCustomErrorAttributes.java @@ -2,6 +2,7 @@ package com.baeldung.web.config; import java.util.Map; +import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; import org.springframework.stereotype.Component; import org.springframework.web.context.request.WebRequest; @@ -10,8 +11,8 @@ import org.springframework.web.context.request.WebRequest; public class MyCustomErrorAttributes extends DefaultErrorAttributes { @Override - public Map getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { - Map errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace); + public Map getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) { + Map errorAttributes = super.getErrorAttributes(webRequest, options); errorAttributes.put("locale", webRequest.getLocale() .toString()); errorAttributes.remove("error"); diff --git a/spring-boot-rest/src/main/resources/application.properties b/spring-boot-rest/src/main/resources/application.properties index 176deb4f49..5017eb2f0d 100644 --- a/spring-boot-rest/src/main/resources/application.properties +++ b/spring-boot-rest/src/main/resources/application.properties @@ -3,3 +3,4 @@ server.servlet.context-path=/spring-boot-rest ### Spring Boot default error handling configurations #server.error.whitelabel.enabled=false #server.error.include-stacktrace=always +#server.error.include-message=always From 14acea1f33d2fbc59300b44776dc22ade450b276 Mon Sep 17 00:00:00 2001 From: Gerardo Roza Date: Tue, 16 Feb 2021 11:45:17 -0300 Subject: [PATCH 2/2] enabled server.error.include-message for 'cause' field to get populated, and using boot properties in MyErrorController setup, plus avoding deprecated method that bypasses error properties' --- .../java/com/baeldung/web/config/MyErrorController.java | 8 ++++---- .../src/main/resources/application.properties | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spring-boot-rest/src/main/java/com/baeldung/web/config/MyErrorController.java b/spring-boot-rest/src/main/java/com/baeldung/web/config/MyErrorController.java index cf3f9c4dbd..05150716f6 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/web/config/MyErrorController.java +++ b/spring-boot-rest/src/main/java/com/baeldung/web/config/MyErrorController.java @@ -4,7 +4,7 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; -import org.springframework.boot.autoconfigure.web.ErrorProperties; +import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.http.HttpStatus; @@ -16,13 +16,13 @@ import org.springframework.web.bind.annotation.RequestMapping; @Component public class MyErrorController extends BasicErrorController { - public MyErrorController(ErrorAttributes errorAttributes) { - super(errorAttributes, new ErrorProperties()); + public MyErrorController(ErrorAttributes errorAttributes, ServerProperties serverProperties) { + super(errorAttributes, serverProperties.getError()); } @RequestMapping(produces = MediaType.APPLICATION_XML_VALUE) public ResponseEntity> xmlError(HttpServletRequest request) { - Map body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.APPLICATION_XML)); + Map body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.APPLICATION_XML)); body.put("xmlkey", "the XML response is different!"); HttpStatus status = getStatus(request); return new ResponseEntity<>(body, status); diff --git a/spring-boot-rest/src/main/resources/application.properties b/spring-boot-rest/src/main/resources/application.properties index 5017eb2f0d..1e985feed9 100644 --- a/spring-boot-rest/src/main/resources/application.properties +++ b/spring-boot-rest/src/main/resources/application.properties @@ -3,4 +3,4 @@ server.servlet.context-path=/spring-boot-rest ### Spring Boot default error handling configurations #server.error.whitelabel.enabled=false #server.error.include-stacktrace=always -#server.error.include-message=always +server.error.include-message=always