使用Spring RestTemplate记录请求和响应

时间:2018-07-18 07:28:22

标签: resttemplate

出现以下错误:

aused by: java.lang.IllegalArgumentException: No InputStream specified
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.util.StreamUtils.copy(StreamUtils.java:118)
    at org.springframework.util.StreamUtils.copyToByteArray(StreamUtils.java:56)
    at org.springframework.http.client.BufferingClientHttpResponseWrapper.getBody(BufferingClientHttpResponseWrapper.java:69)  

用于记录请求和响应的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

public class LoggingRequestInterceptor implements ClientHttpRequestInterceptor {    
    private static final String CLASS_NAME = LoggingRequestInterceptor.class.getName();         
    private static final String LOG_NAME = "WBDO2Web";

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        traceRequest(request, body);
        ClientHttpResponse response = execution.execute(request, body);
        traceResponse(response);
        return response;
    }

    private void traceRequest(HttpRequest request, byte[] body) throws IOException {
        String methodName="traceRequest";
        LogUtil.w2Info(CLASS_NAME, methodName,"===========================request begin================================================",LOG_NAME);
        LogUtil.w2Info(CLASS_NAME, methodName,"URI         : {}"+ request.getURI(),LOG_NAME);
        LogUtil.w2Info(CLASS_NAME, methodName,"Method      : {}"+ request.getMethod(),LOG_NAME);
        LogUtil.w2Info(CLASS_NAME, methodName,"Headers     : {}"+ request.getHeaders(),LOG_NAME);
        LogUtil.w2Info(CLASS_NAME, methodName,"Request body: {}"+ new String(body, "UTF-8"),LOG_NAME);
        LogUtil.w2Info(CLASS_NAME, methodName,"==========================request end================================================",LOG_NAME);
    } 

    private void traceResponse(ClientHttpResponse response) throws IOException {
        String methodName="traceResponse";
        System.out.println("Response#########"+response.getBody());
        final ClientHttpResponse responseWrapper = new BufferClientHttpResponseWrapper(response);
        StringBuilder inputStringBuilder = new StringBuilder();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(responseWrapper.getBody(), "UTF-8"));
        String line = bufferedReader.readLine();

        while (line != null) {
            inputStringBuilder.append(line);
            inputStringBuilder.append('\n');
            line = bufferedReader.readLine();
        }

        LogUtil.w2Info(CLASS_NAME, methodName,"============================response begin==========================================",LOG_NAME);
        LogUtil.w2Info(CLASS_NAME, methodName,"Status code  : {}"+ response.getStatusCode(),LOG_NAME);
        LogUtil.w2Info(CLASS_NAME, methodName,"Status text  : {}"+ response.getStatusText(),LOG_NAME);
        LogUtil.w2Info(CLASS_NAME, methodName,"Headers      : {}"+ response.getHeaders(),LOG_NAME);
        LogUtil.w2Info(CLASS_NAME, methodName,"Response body: {}"+ inputStringBuilder.toString(),LOG_NAME);
        LogUtil.w2Info(CLASS_NAME, methodName,"=======================response end=================================================",LOG_NAME);
    }
}

0 个答案:

没有答案