Spring Boot custom ErrorAttributes http status not set to response

南笙酒味 提交于 2021-01-21 11:00:54

问题


Following Spring Boot documentation I defined my own ErrorAttributes bean (see below), I was able to make the json response to show the information I wanted, including my own error code and message by using a custom exception to wrap that information and generate the error response from it. The only issue with this is that the http status of the response is not matching the one I define in the status attribute, it is not been overridden.

@Bean
public ErrorAttributes errorAttributes() {
    return new DefaultErrorAttributes() {
        @Override
        public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {

            Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);

            Throwable error = getError(requestAttributes);

            if (error instanceof MyException) {
                MyException myException = (MyException) error;

                errorAttributes.put("errorCode", myException.getErrorCode());
                errorAttributes.put("message", myException.getMessage());
                errorAttributes.put("status", myException.getStatus());

                HttpStatus correspondentStatus = HttpStatus.valueOf(myException.getStatus());
                errorAttributes.put("error", correspondentStatus.getReasonPhrase());
            }

            return errorAttributes;
        }
    };
}

The response's http status is not matching the status in the json, for example:

HTTP/1.1 500 
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 01 Mar 2017 18:48:22 GMT
{
   "timestamp": "2017-03-01T18:48:21.894+0000",
   "status": 403,
   "error": "Forbidden",
   "exception": "com.myapp.MyException",
   "message": "You are not authorized. This user doesn't exist in the db",
   "path": "/account",
   "errorCode": "00013"
}

回答1:


All you are doing is building the body of your error response, as you can see from your sample. Spring is the one handling the status code.

If you want to have full control on all parts of the response then you should use the ControllerAdvice approach as shown in their documentation:

@ControllerAdvice(basePackageClasses = FooController.class)
public class FooControllerAdvice extends ResponseEntityExceptionHandler {

   @ExceptionHandler(MyException.class)
   public ResponseEntity<Message> handleRequestErrorMyException(HttpServletRequest request, MyException myException) {
        HttpStatus status = HttpStatus.valueOf(myException.getStatus();
        return new ResponseEntity<>(new CustomErrorType(status.value(), ex.getMessage()), status);
    }

}

With this bean in place all MyException thrown by any controller under the FooController package will be captured and processed by handleRequestErrorMyException, the response to the original request will be the one returned by this method. Just make sure in your Configuration class that this package gets scanned.




回答2:


I found a way of setting the http status from within the logic that creates my custom ErrorAttributes bean, this way I am able to re-use the out of the box Spring Boot error response creation and update it with my custom information without the need of exception handlers and controller advices.

By adding the next line you can set the http status which overrides the current one in the requestAttributes.

requestAttributes.setAttribute("javax.servlet.error.status_code", httpStatus, 0);

Where httpStatus is the status you want to set.

Here is the full bean definition with the added line:

@Bean
public ErrorAttributes errorAttributes() {
    return new DefaultErrorAttributes() {
        @Override
        public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {

            Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);

            Throwable error = getError(requestAttributes);

            if (error instanceof MyException) {
                MyException myException = (MyException) error;
                int httpStatus = myException.getStatus();

                errorAttributes.put("errorCode", myException.getErrorCode());
                errorAttributes.put("message", myException.getMessage());
                errorAttributes.put("status", httpStatus);

                HttpStatus correspondentStatus = HttpStatus.valueOf(httpStatus);
                errorAttributes.put("error", correspondentStatus.getReasonPhrase());
                requestAttributes.setAttribute("javax.servlet.error.status_code", httpStatus, 0);
            }

            return errorAttributes;
        }
    };
}

How did I find it? By looking at the DefaultErrorAttributes class, I found there is a method addStatus which is private, but it shows the name of the attribute that is used by the code to generate the response's http-status, that was the clue I was looking for:

private void addStatus(Map<String, Object> errorAttributes, RequestAttributes requestAttributes) {
    Integer status = (Integer)this.getAttribute(requestAttributes, "javax.servlet.error.status_code");
...

Looking more into the code I found that the getAttribute method that is being called there is actually calling the method from RequestAttributes interface:

private <T> T getAttribute(RequestAttributes requestAttributes, String name) {
    return requestAttributes.getAttribute(name, 0);
}

Checking inside that interface I found there is also a setAttribute method. It worked.

HTTP/1.1 403 
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 01 Mar 2017 20:59:33 GMT
{
   "timestamp": "2017-03-01T20:59:32.774+0000",
   "status": 403,
   "error": "Forbidden",
   "exception": "com.myapp.MyException",
   "message": "You are not authorized. This user doesn't exist in the db",
   "path": "/account",
   "errorCode": "00013"
}



回答3:


The accepted answer didn't work for me in a Spring Boot 2.3.0 application, I had to subclass ErrorController to override the original status.

This is the code of BasicErrorController, applied as default:

    @RequestMapping
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        HttpStatus status = getStatus(request);
        if (status == HttpStatus.NO_CONTENT) {
            return new ResponseEntity<>(status);
        }
        Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
        return new ResponseEntity<>(body, status);
    }

As you can see the original status is kept on a variable gotten before the invocation of getErrorAttributes(). Thus, adding requestAttributes.setAttribute("javax.servlet.error.status_code", httpStatus, 0); in your custom getErrorAttibutes() doesn't really do anything.

In a custom extension of BasicErrorController (remember to add it as a bean) you can override error() and make sure status gets the value you want:

public class CustomBasicErrorController extends BasicErrorController {

    public CustomBasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
        super(errorAttributes, errorProperties);
    }

    public CustomBasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List<ErrorViewResolver> errorViewResolvers) {
        super(errorAttributes, errorProperties, errorViewResolvers);
    }

    @Override
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
        Integer status = (Integer) body.get("status");
        if (status == HttpStatus.NO_CONTENT.value()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity<>(body, HttpStatus.valueOf(status));
    }
}



回答4:


Alternatively you can use

MyException extends ResponseStatusException {
 public MyException (String msg) {
 super(HttpStatus.FORBIDDEN, msg);
} 



回答5:


@Getter
public class AppException extends ResponseStatusException {
  private final ErrorAttributeOptions options;

  public AppException(HttpStatus status, String message, ErrorAttributeOptions options){
      super(status, message);
      this.options = options;
  }
}
  1. Use sendError with status in ExceptionHandler:

    @ExceptionHandler(AppException .class)
    public void appException(HttpServletResponse response) throws IOException {
         response.sendError(ex.getStatus().value());
    }
    

See Spring REST Error Handling Example

  1. You can crossbreed ResponseEntityExceptionHandler with DefaultErrorAttributes like this (Spring Boot 2.3 comes with additional ErrorAttributeOptions):
@RestControllerAdvice
@AllArgsConstructor
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
    private final ErrorAttributes errorAttributes;

    @ExceptionHandler(AppException.class)
    public ResponseEntity<Map<String, Object>> appException(AppException ex, WebRequest request) throws IOException {
        Map<String, Object> body = errorAttributes.getErrorAttributes(request, ex.getOptions());
        HttpStatus status = ex.getStatus();
        body.put("status", status.value());
        body.put("error", status.getReasonPhrase());
        return ResponseEntity.status(status).body(body);
    }
}

I've checked it for MESSAGE, EXCEPTION and STACK_TRACE options.
See also Using ErrorAttributes in our custom ErrorController



来源:https://stackoverflow.com/questions/42541520/spring-boot-custom-errorattributes-http-status-not-set-to-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!