Spring ClientHttpRequestInterceptor request/response 空
Spring ClientHttpRequestInterceptor request/response empty
我想获取 REST 请求和响应主体,以便在我的应用程序中进行日志记录。
我目前有一个实现 ClientHttpRequestInterceptor 的 loggingRequestInterceptor class,与 sofiene here.
的回答非常相似
然后我在 spring 配置的 Rest 模板中将此拦截器添加为 属性。
我的代码如下所示:
public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor {
private static final Logger logger = LoggerFactory.getLogger(LoggingRequestInterceptor.class);
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
ClientHttpResponse response = execution.execute(request, body);
response = log(request, body, response);
return response;
}
private ClientHttpResponse log(final HttpRequest request, final byte[] body, final ClientHttpResponse response) throws IOException {
final ClientHttpResponse responseCopy = new BufferingClientHttpResponseWrapper(response);
StringBuilder builder = new StringBuilder();
builder.append("Method: ").append(request.getMethod().toString());
builder.append("URI: ").append(request.getURI().toString());
builder.append("Request Body: ").append(new String(body,"UTF-8"));
builder.append("Response body: ").append(convertStreamToString(responseCopy.getBody()));
logger.info(builder.toString());
return responseCopy;
}
}
Spring 属性 在我的 RestTemplate 上:
<property name="interceptors">
<list>
<bean class="com.company.projectName.service.api.rest.io.impl.LoggingRequestInterceptor" />
</list>
</property>
但是,在我所有的休息调用中,我记录了方法和 URI,但是正文和响应正文是空的。从请求和响应中获取正文以允许我的应用程序记录此信息的正确方法是什么。
解决方法:首先,请求体是空白的,因为这是一个GET方法。其次,我还需要记录 ResponseStatusCode,因为我收到的状态为 204,即无内容,因此导致响应主体为空白。
我想获取 REST 请求和响应主体,以便在我的应用程序中进行日志记录。
我目前有一个实现 ClientHttpRequestInterceptor 的 loggingRequestInterceptor class,与 sofiene here.
的回答非常相似然后我在 spring 配置的 Rest 模板中将此拦截器添加为 属性。
我的代码如下所示:
public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor {
private static final Logger logger = LoggerFactory.getLogger(LoggingRequestInterceptor.class);
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
ClientHttpResponse response = execution.execute(request, body);
response = log(request, body, response);
return response;
}
private ClientHttpResponse log(final HttpRequest request, final byte[] body, final ClientHttpResponse response) throws IOException {
final ClientHttpResponse responseCopy = new BufferingClientHttpResponseWrapper(response);
StringBuilder builder = new StringBuilder();
builder.append("Method: ").append(request.getMethod().toString());
builder.append("URI: ").append(request.getURI().toString());
builder.append("Request Body: ").append(new String(body,"UTF-8"));
builder.append("Response body: ").append(convertStreamToString(responseCopy.getBody()));
logger.info(builder.toString());
return responseCopy;
}
}
Spring 属性 在我的 RestTemplate 上:
<property name="interceptors">
<list>
<bean class="com.company.projectName.service.api.rest.io.impl.LoggingRequestInterceptor" />
</list>
</property>
但是,在我所有的休息调用中,我记录了方法和 URI,但是正文和响应正文是空的。从请求和响应中获取正文以允许我的应用程序记录此信息的正确方法是什么。
解决方法:首先,请求体是空白的,因为这是一个GET方法。其次,我还需要记录 ResponseStatusCode,因为我收到的状态为 204,即无内容,因此导致响应主体为空白。