Spring Boot自定义http错误响应?

时间:2014-10-07 13:08:12

标签: java rest error-handling spring-boot

如果Spring Boot Web应用程序中发生异常,如何自定义响应状态代码和响应正文中的数据?

我创建了一个Web应用程序,如果由于某些错误的内部状态而发生意外情况,则会引发自定义异常。因此,触发错误的请求的响应主体看起来像:

HTTP/1.1 500 Internal Server Error
{
    "timestamp": 1412685688268,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "com.example.CustomException",
    "message": null,
    "path": "/example"
}

现在,我想更改状态代码并设置响应正文中的字段。我想到的一个解决方案是:

@ControllerAdvice
class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    ErrorMessage handleBadCredentials(CustomException e) {
        return new ErrorMessage("Bad things happened");
    }
}

@XmlRootElement
public class ErrorMessage(
    private String error;

    public ErrorMessage() {
    }

    public ErrorMessage(String error) {
        this.error = error;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
)

然而,这创造了(如所怀疑的)完全不同的反应:

HTTP/1.1 400 Bad Request
{
    "error": "Bad things happened"
}

2 个答案:

答案 0 :(得分:10)

正如@zeroflagL所提到的,Spring Boot制定了"标准" org.springframework.boot.autoconfigure.web.DefaultErrorAttributes中的错误响应正文。与您的需求类似,我想充分利用所有这些,但只需再增加一个"类型"我的一些例外提供的字段。

我是通过实施Component分类DefaultErrorAttributes来实现的。 Spring Boot自动将其拾取并使用我的而不是默认值。

@Component
public class ExtendedErrorAttributes extends DefaultErrorAttributes {
    @Override
    public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
        final Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);

        final Throwable error = super.getError(requestAttributes);
        if (error instanceof TypeProvider) {
            final TypeProvider typeProvider = (TypeProvider) error;
            errorAttributes.put("type", typeProvider.getTypeIdentifier());
        }

        return errorAttributes;
    }
}

有了这个,我得到了一个增强的JSON响应体,比如

{
  "timestamp": 1488058582764,
  "status": 429,
  "error": "Too Many Requests",
  "exception": "com.example.ExternalRateLimitException",
  "message": "DAILY_LIMIT: too many requests",
  "path": "/api/lookup",
  "type": "DAILY_LIMIT"
}

答案 1 :(得分:7)

可以使用HttpServletResponse.sendError(int)方法更改http响应状态代码,例如

@ExceptionHandler
void handleIllegalArgumentException(IllegalArgumentException e, HttpServletResponse response) throws IOException {
    response.sendError(HttpStatus.BAD_REQUEST.value());
}

或者,如果您有两个或更多异常来生成相同的响应状态,则可以在@ExceptionHandler注释中声明异常类型:

@ExceptionHandler({IllegalArgumentException.class, NullPointerException.class})
void handleBadRequests(HttpServletResponse response) throws IOException {
    response.sendError(HttpStatus.BAD_REQUEST.value());
}

可以在我的blog post中找到更多信息。