Hutool工具包中HttpUtil的日志统一打印 为何要打印Http请求日志HttpUtil的请求拦截器(HttpInterceptor.Chain)、响应拦截器(HttpIntercept
使用hutool工具包中的HttpUtil,为了便于排查问题以及控制请求时间,每次都要在请求前后log日志,每次都需要设置超时时间,十分麻烦。
log.info("请求路径:{},请求体:{}", url, body); HttpResponse response = HttpUtil.createPost(url).body(body).timeout(3000).execute(); log.info("请求结果:{}", response);
从HttpUtil的execute()方法点进去几步,可以看到以下代码
final HttpInterceptor.Chain<HttpRequest> requestInterceptors = GlobalInterceptor.INSTANCE.getCopiedRequestInterceptor();final HttpInterceptor.Chain<HttpResponse> responseInterceptors = GlobalInterceptor.INSTANCE.getCopiedResponseInterceptor();public HttpResponse execute(boolean isAsync) {return doExecute(isAsync, config.requestInterceptors, config.responseInterceptors);}
这里有两个拦截器配置,分别是请求拦截器配置config.requestInterceptors, 响应拦截器配置config.responseInterceptors
继续点进去可以看到拦截器的执行逻辑,分别在请求前和请求后执行,代码进行省略,只保留我们需要的逻辑
private HttpResponse doExecute(boolean isAsync, HttpInterceptor.Chain<HttpRequest> requestInterceptors, HttpInterceptor.Chain<HttpResponse> responseInterceptors) {if (null != requestInterceptors) {for (HttpInterceptor<HttpRequest> interceptor : requestInterceptors) {interceptor.process(this);}}// 初始化URLurlWithParamIfGet();// 初始化 connectioninitConnection();// 发送请求send();// 手动实现重定向HttpResponse httpResponse = sendRedirectIfPossible(isAsync);// 获取响应if (null == httpResponse) {httpResponse = new HttpResponse(this.httpConnection, this.config, this.charset, isAsync, isIgnoreResponseBody());}// 拦截响应if (null != responseInterceptors) {for (HttpInterceptor<HttpResponse> interceptor : responseInterceptors) {interceptor.process(httpResponse);}}return httpResponse;}
明白了HttpUtil的execute()方法执行过程,现在我们可以很方便的实现HttpUtil的全局日志了
项目启动时进行添加我们自定义的拦截器即可,这里直接使用toString方法,是因为HttpUtil里面帮我们重写了toString的逻辑,不需要我们自己去解析HttpRequest和HttpResponse了
@Autowired public void addRequestInterceptor() { GlobalInterceptor.INSTANCE.addRequestInterceptor(httpObj -> log.info(String.valueOf(httpObj))); GlobalInterceptor.INSTANCE.addResponseInterceptor(httpObj -> log.info(String.valueOf(httpObj))); }
同样的,我们点进timeout()方法可以发现,会分别设置连接超时、读取响应超时。
int connectionTimeout = HttpGlobalConfig.getTimeout();int readTimeout = HttpGlobalConfig.getTimeout();public HttpConfig timeout(int milliseconds) {setConnectionTimeout(milliseconds);setReadTimeout(milliseconds);return this;}public HttpConfig setConnectionTimeout(int milliseconds) {this.connectionTimeout = milliseconds;return this;}public HttpConfig setReadTimeout(int milliseconds) {this.readTimeout = milliseconds;return this;}
但是这两个超时字段配置是由默认的全局配置的。
所以配置一个全局的超时时间就比较方便了
@Autowired public void hutoolHttpUtilConfig() { // 设置hutool HttpUtil的request请求参数打印 GlobalInterceptor.INSTANCE.addRequestInterceptor(httpObj -> log.info(String.valueOf(httpObj))); // 设置hutool HttpUtil的response参数打印 GlobalInterceptor.INSTANCE.addResponseInterceptor(httpObj -> log.info(String.valueOf(httpObj))); // 设置hutool HttpUtil的连接超时时间、读取响应超时时间 HttpGlobalConfig.setTimeout(3000); }
来源地址:https://blog.csdn.net/ayuyihu/article/details/129958670
--结束END--
本文标题: Hutool工具包中HttpUtil的日志统一打印以及统一超时时间配置
本文链接: https://lsjlt.com/news/397125.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0