如何在过滤器中将异常序列化为json?

时间:2018-10-11 13:48:13

标签: spring-boot jhipster

在过滤器的方法中,我根据不同的条件抛出两个不同的异常:

throw new InvalidRequestException("no access token in Authorization");

throw new InvalidTokenException("access token has expired, but there's no refresh token");

我设法触发了两个异常,但邮递员的回答却不同: 对于InvalidRequestException:

{
    "timestamp": "2018-10-11T13:36:55.781+0000",
    "status": 401,
    "error": "Unauthorized",
    "message": "Unauthorized",
    "path": "/api/account"
}

对于InvalidTokenException:

{
    "error": "invalid_token",
    "error_description": "Access token expired: token details...."
}

我检查了两个异常都扩展了ClientAuthenticationException,为什么响应格式不同?

如何为过滤器中抛出的异常定制异常处理程序?

1 个答案:

答案 0 :(得分:0)

这可能是由于JHipster如何为Zalando的问题库转换某些已知的异常类型。参见ExceptionTranslator.java