如果你只想在
InvalidUserInputException
则可以编写异常处理程序:
@ExceptionHandler(InvalidUserInputException.class)
@ResponseBody
public ValidationError getError(InvalidUserInputException ex) {
}
自从
InvalidUserInputException无效用户输入异常
Object
您可以在其中传递请求正文,例如:
throw new InvalidUserInputException("Wrong email format", loginOwner);
现在您可以使用
toString()
Owner
Apache commons
以及他们的
ToStringBuilder
:
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.JSON_STYLE)
.append("email", email)
.append("password", password)
.toString();
}
toString()
HttpServletRequest
作为异常处理程序的参数。
ContentCachingRequestWrapper
.
总而言之,你应该自己写
Filter
。您可以通过从弹簧
OncePerRequestFilter
ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper) request;
wrapper.getContentAsByteArray();
@Component
public class RequestWrapperFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
filterChain.doFilter(new ContentCachingRequestWrapper(httpServletRequest), httpServletResponse);
}
}
getContentAsByteArray()
IOUtils
来自Apache Commons:
ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper) request;
logger.warn("Input validation failed, request body: {}", IOUtils.toString(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding()));
这将把整个请求主体记录为一个字符串。但要小心记录敏感信息。