Я рекомендую реализовать фильтр для преобразования или использования информации, содержащейся в запросах или ответах. а не Interceptor, он предоставляет больше информации, чем Interceptor. Вот пример использования Filter для регистрации:
@Component
public class HttpLoggingFilter implements Filter {
private static final Logger logger = LoggerFactory.getLogger(HttpLoggingFilter.class);
@Value("${output.trace.actif}")
private boolean isOutputActif;
private static String getRequestData(final HttpServletRequest request) throws UnsupportedEncodingException {
String payload = null;
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
}
}
return payload;
}
private static String getResponseData(final HttpServletResponse response) throws IOException {
String payload = null;
ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
if (wrapper != null) {
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
wrapper.copyBodyToResponse();
}
}
return payload;
}
@Override
public void init(FilterConfig filterConfig) {
logger.info("start http filter");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
ContentCachingRequestWrapper requestToCache = new ContentCachingRequestWrapper(httpServletRequest);
ContentCachingResponseWrapper responseToCache = new ContentCachingResponseWrapper(httpServletResponse);
HttpUtil.majMDCRestInfo(httpServletRequest);
long start = System.currentTimeMillis();
chain.doFilter(requestToCache, responseToCache);
long elapsedTime = System.currentTimeMillis() - start;
String requestBody = new String(requestToCache.getContentAsByteArray());
String responseBody = new String(responseToCache.getContentAsByteArray());
final StringBuilder logMessage = new StringBuilder().append("[METHOD:").append(httpServletRequest.getMethod())
.append("] [PARAMS:")
.append(httpServletRequest.getQueryString()).append("] [BODY:").append(requestBody).append("]");
if (isOutputActif) {
String respContent = responseBody;
if (respContent.equals("")) {
respContent = "no data";
}
logMessage.append(" [RESPONSE:").append(respContent).append("]");
}
logMessage.append(" [STATUS:").append(responseToCache.getStatus()).append("] [Time:").append(elapsedTime).append("ms]");
String[] nonLoggingPaths = {"/api/"};
String urlPath = httpServletRequest.getRequestURL().toString();
if ((Arrays.stream(nonLoggingPaths).parallel().anyMatch(urlPath::contains))) {
logger.info("{}", logMessage);
}
getRequestData(requestToCache);
getResponseData(responseToCache);
}
}