【问题标题】:mget producing: Unable to parse response body for Responsemget 生产:无法解析响应的响应正文
【发布时间】:2020-06-17 15:06:22
【问题描述】:

我正在通过 Java 高级 REST 客户端发出 Multi-Get request 并收到以下异常:

“无法解析 Response{requestLine=POST /_mget HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK} 的响应正文”

我从发送到 Elastic 的日志中提取了以下 JSON:

{
    "docs": [
        {
            "_index": "blah",
            "_type": null,
            "_id": "some-id-232332",
            "routing": null,
            "stored_fields": null,
            "version": -3,
            "version_type": "internal",
            "_source": {
                "includes": [],
                "excludes": []
            }
        }
    ]
}

我通过 Postman 将上述 JSON 发送到 Elastic,我看到以下响应(与我在日志中看到的相同):

{
    "docs": [
        {
            "_index": "blah",
            "_type": null,
            "_id": "some-id-232332",
            "found": false
        }
    ]
}

这不是一个有效的回应吗?这是 elasticsearch-rest-high-level-client 的问题吗?

弹性 7.5.0,org.elasticsearch.client:elasticsearch-rest-high-level-client:7.5.2

【问题讨论】:

  • 您能否添加更多详细信息,例如您的请求或您的要求?
  • 您能否以 JSON 格式提供您的搜索查询、映射和示例文档,您的 rest-client java 代码,以便我们重现问题并为您提供帮助
  • 嗨@OpsterElasticsearchNinja - 我希望避免创建一个简化的示例,因为发布我们的代码有很多不适用的东西。不过,我设法找到了它。我已经创建了一个示例,请查看我的答案。
  • @spottedmahn,将通过它,感谢您跟进此

标签: elasticsearch resthighlevelclient mget


【解决方案1】:

我需要检查getCause() 的异常以找出真正的问题。我在mapper.readValue(nullBytes, Customer.class); 上将null 传递给Jackson 是真正的问题。

够有趣的a NPE shows itself ?‍♂️

堆栈跟踪: java.util.concurrent.ExecutionException: java.io.IOException: 无法解析响应的响应正文{requestLine=POST /_mget HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}
...
...
真正的问题在这里!!! ??
引起:java.lang.IllegalArgumentException:参数“src”为空 在 com.fasterxml.jackson.databind.ObjectMapper._assertNotNull(ObjectMapper.java:4429)

restHighLevelClient.mgetAsync(multiGetRequest, RequestOptions.DEFAULT, new ActionListener<MultiGetResponse>() {
    @Override
    public void onResponse(MultiGetResponse response) {
      for (var responseItem : response.getResponses()) {
        try {
          // simulating a null source
          byte[] nullBytes = null;
          customer = mapper.readValue(nullBytes, Customer.class);
        } catch (IOException e) {
          result.completeExceptionally(e);
        }
      }
      result.complete(true);
    }

    @Override
    public void onFailure(Exception ex) {
      //the real problem!!!
      //log.error("ex.cause", ex.getCause());
      log.error("error with mget", ex);
      result.completeExceptionally(ex);
    }
  });

Full source of repro, Full log file

【讨论】:

  • 很好的解释+1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-11
相关资源
最近更新 更多