升级Spring Boot后调用repository.count()报JSON解析异常求助
Spring Boot 2.7.18升级后ElasticsearchRepository.count()报json_parse_exception排查思路
问题背景
将应用从Spring Boot 2.0.2升级至2.7.18后,调用ElasticsearchRepository.count()时抛出JSON解析异常:
RestStatusException{status=500} org.springframework.data.elasticsearch.RestStatusException: Elasticsearch exception [type=json_parse_exception, reason=Current token (VALUE_NUMBER_INT) not of boolean type at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@79b99ce2; line: 1, column: 137]]; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=json_parse_exception, reason=Current token (VALUE_NUMBER_INT) not of boolean type
触发异常的核心代码:
@Repository public interface MetadataWithIdRepository extends ElasticsearchRepository<MetadataWithId, String> {} // 注入后调用 repository.count() // 此处抛出异常
环境关键信息:
- 集成测试使用
org.testcontainers.elasticsearch.ElasticsearchContainer(1.19.3版本),镜像为docker.elastic.co/elasticsearch/elasticsearch:6.5.1 - 新旧依赖版本对比:
| 依赖项 | 旧版本(Spring Boot 2.0.2) | 新版本(Spring Boot 2.7.18) |
|---|---|---|
| Spring Boot | 2.0.2 | 2.7.18 |
| Spring Framework | 5.0.6 | 5.3.31 |
| Spring Cloud | Finchley.SR1 | 2021.0.3 |
| Spring Data Elasticsearch | 3.0.7 | 4.4.18 |
| Elasticsearch客户端依赖 | 5.6.9 | 7.17.15 |
| Elasticsearch实例版本 | 6.5.1 | 6.5.1 |
调试确认索引状态、基础查询均正常,但count()方法调用失败。
排查思路
1. 优先验证版本兼容性(核心嫌疑)
Spring Data Elasticsearch 4.4.x(对应Spring Boot 2.7.x)官方兼容的Elasticsearch实例版本为7.17.x,当前客户端(7.17.15)与服务端(6.5.1)版本跨度极大,API协议、参数格式存在不兼容:
- 部分请求参数在ES 6.x中为数字类型,但7.x客户端期望布尔类型,导致服务端返回的响应无法被正确解析
- 验证方式:将Testcontainers的ES镜像替换为
7.17.15,重新运行测试观察异常是否消失
2. 抓取请求日志分析格式差异
开启Elasticsearch请求日志,查看count()方法发送的具体请求体,对比ES 6.5.1与7.17.x的API文档差异:
在application.yml中添加日志配置:
logging: level: org.springframework.data.elasticsearch.client.WIRE: DEBUG
重点检查请求参数中是否存在"数字/布尔类型不匹配"的字段(比如terminate_after、ignore_unavailable等参数的格式变化)
3. 检查索引映射兼容性
升级后的Spring Data Elasticsearch可能生成与ES 6.x不兼容的映射结构,执行以下命令查看当前索引映射:
GET localhost:56229/document-domain/_mapping
对比旧版本应用生成的映射,重点排查字段类型、doc_values/index等属性的变化是否导致解析异常
4. 适配Testcontainers版本
Testcontainers 1.19.3对ES 7.x的支持可能存在隐性问题,尝试升级Testcontainers至Spring Boot 2.7.x推荐的兼容版本(如1.17.x或更高),避免容器通信时的版本适配问题
内容的提问来源于stack exchange,提问作者fafarafa
相关产品推荐
相关产品推荐

