Я создал пользовательское исключение @ResponseStatus
и хочу, чтобы оно возвращалось как ответ json для @RestController
, а также полезная нагрузка ответа.
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class BadRequestException extends NestedRuntimeException {
public BadRequestException(String msg) {
super(msg);
}
}
У меня есть CommonsRequestLoggingFilter
, который должен регистрировать ответ после его отправки. Поэтому фильтр читает wrapper.getContentAsByteArray()
:
public class CustomLoggingFilter extends CommonsRequestLoggingFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
ContentCachingResponseWrapper responseToUse = new ContentCachingResponseWrapper(response);
try {
super.doFilterInternal(request, responseToUse, filterChain);
} finally {
byte[] buf = responseToUse.getContentAsByteArray();
String message;
//PROBLEM: buf.length() == 0 in error case, thus cannot create the message
LOGGER.info("Response: " + message);
}
}
}
Обычно ведение журнала работает, но в случае ошибки @ResponseStatus
тело ответа / полезные данные теряются!
Вопрос: как сохранить полезную нагрузку тела для регистрации во время исключения?