Merge pull request #10493 from rozagerardo/rozagerardo/JAVA-4511_Update-Error-Handling-for-REST-article

[JAVA-4511] Update "Error Handling for REST" article
This commit is contained in:
Loredana Crusoveanu 2021-02-21 14:56:21 +02:00 committed by GitHub
commit ef5f90a6ca
3 changed files with 8 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package com.baeldung.web.config;
import java.util.Map; import java.util.Map;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest; import org.springframework.web.context.request.WebRequest;
@ -10,8 +11,8 @@ import org.springframework.web.context.request.WebRequest;
public class MyCustomErrorAttributes extends DefaultErrorAttributes { public class MyCustomErrorAttributes extends DefaultErrorAttributes {
@Override @Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace); Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, options);
errorAttributes.put("locale", webRequest.getLocale() errorAttributes.put("locale", webRequest.getLocale()
.toString()); .toString());
errorAttributes.remove("error"); errorAttributes.remove("error");

View File

@ -4,7 +4,7 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest; 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.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -16,13 +16,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
@Component @Component
public class MyErrorController extends BasicErrorController { public class MyErrorController extends BasicErrorController {
public MyErrorController(ErrorAttributes errorAttributes) { public MyErrorController(ErrorAttributes errorAttributes, ServerProperties serverProperties) {
super(errorAttributes, new ErrorProperties()); super(errorAttributes, serverProperties.getError());
} }
@RequestMapping(produces = MediaType.APPLICATION_XML_VALUE) @RequestMapping(produces = MediaType.APPLICATION_XML_VALUE)
public ResponseEntity<Map<String, Object>> xmlError(HttpServletRequest request) { public ResponseEntity<Map<String, Object>> xmlError(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.APPLICATION_XML)); Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.APPLICATION_XML));
body.put("xmlkey", "the XML response is different!"); body.put("xmlkey", "the XML response is different!");
HttpStatus status = getStatus(request); HttpStatus status = getStatus(request);
return new ResponseEntity<>(body, status); return new ResponseEntity<>(body, status);

View File

@ -3,3 +3,4 @@ server.servlet.context-path=/spring-boot-rest
### Spring Boot default error handling configurations ### Spring Boot default error handling configurations
#server.error.whitelabel.enabled=false #server.error.whitelabel.enabled=false
#server.error.include-stacktrace=always #server.error.include-stacktrace=always
server.error.include-message=always