You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot自定义响应式解码器InputStream字符丢失问题排查

JSON-LD解码器请求数据截断问题及临时解决方法

更新

如果对传递给requests客户端的字符串调用encode()方法,问题即可解决。

response: Response = requests.request('POST', 
   "http://localhost:8080/api", 
   headers={
     'Accept': "application/ld+json",
     'Content-Type': "application/ld+json" 
   }, 
   data=content.encode()
)

我不太理解将字符串编码为UTF-8为何会改变服务器端的行为(Wireshark显示完整字符串已传输),但目前该方法有效。


原问题

我实现了一个自定义解码器用于解析请求中的JSON-LD数据:

public Mono<Foo> decodeToMono(Publisher<DataBuffer> buffers, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
  return DataBufferUtils.join(buffers)
            .flatMap(dataBuffer -> {
                try (InputStream is = dataBuffer.asInputStream(true)) {
                    var result = new String(is.readAllBytes(), StandardCharsets.UTF_8);
                    log.info(result);

                    // some rdf logic here
                    return Mono.just(...);
                } catch (Exception e) {
                    log.warn("Failed to parse request of mimetype '{}'", mimeType);
                    return Mono.error(e);
                }
            });
}

我使用Python requests库调用该API。正常情况下日志应打印完整的JSON文档(简化示例如下):

[   
    {
    "@id": "_:Nf3d46019bcde490484128dfd01ebca40",
    "@type": [
      "https://schema.org/DefinedTerm"
    ],
    "https://schema.org/identifier": [
      {
        "@value": "tag:arithemtica+integra"
      }
    ],
    "https://schema.org/termCode": [
      {
        "@value": "Arithemtica integra"
      }
    ]
  }
]

但Java解码器中打印的字符串会被截断,示例如下:

[   
    {
    "@id": "_:Nf3d46019bcde490484128dfd01ebca40",
    "@type": [
      "https://schema.org/DefinedTerm"
    ],
    "https://schema.org/identifier": [
      {
        "@value": "tag:arithemtica+integra"
      }
    ],
    "https://schema.org/termCode": [
      {
        "@value": "Arithemt

由于末尾字符丢失,解码时会抛出异常:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Unexpected end-of-input: expected close marker for Array (start marker at [Source: (StringReader); line: 1, column: 6350])

截断位置看似随机,但总是接近字符串末尾(例如有效JSON共6356字符,截断在6350位)。

我猜测问题与流提前截断有关,但不知从何处入手排查根本原因。

内容的提问来源于stack exchange,提问作者Xogaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 10:37:26