Jackson 的 ObjectMapper 失败:套接字关闭

问题描述

我正在使用 Apache 的 HttpClient 来启动这样的任务:

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.methods.HttpGet;

HttpClient client = HttpClientBuilder.create().disableRedirectHandling().build();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();

此请求包含用于标识与请求相关的任务的 id。我使用 Jackson ObjectMapper 提取它。

import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);

HttpEntity entity = res.getEntity();
JobStatusModel status = mapper.readValue(entity.getContent(),JobStatusModel.class);
String taskId = status.getId();

这没有问题,我在整个代码中多次使用它。我可以通过这种方式提取 taskId,没有任何问题,并使用重复调用不同的 REST API 来确定任务何时完成以获取任务状态。

当我尝试提取已完成任务的结果时发生错误。这是通过对不同 URL 的类似请求完成的:

HttpGet request = new HttpGet(url);
HttpResponse response2 = client.execute(request);
HttpEntity entity2 = response2.getEntity();
JobResultModel status = mapper.readValue(entity2.getContent(),JobResultModel.class);
request.releaseConnection();

最后一行失败,出现一个非常无用的异常:

java.net.SocketException: socket closed

我尝试了很多方法来读取响应实体,看看有什么问题。我已经尝试使用 Postman 进行完全相同的调用,并且成功且没有问题。该错误与错误的 JobResultModel 形式无关。使用 DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES 的配置 ObjectMapper,它甚至应该允许错误。

非常感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...