【问题标题】:org.springframework.core.codec.DecodingException: JSON decoding error: Illegal character ((CTRL-CHAR, code 31))org.springframework.core.codec.DecodingException:JSON 解码错误:非法字符((CTRL-CHAR,代码 31))
【发布时间】:2020-12-15 21:49:16
【问题描述】:

通过 WebClient(org.springframework.web.reactive.function.client) 获取 post 方法的响应时出现以下错误

org.springframework.core.codec.DecodingException: JSON decoding error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens; nested exception is com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
at [Source: (io.netty.buffer.ByteBufInputStream); line: 1, column: 2]
at org.springframework.http.codec.json.AbstractJackson2Decoder.processException(AbstractJackson2Decoder.java:215)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Ȁ

我尝试使用 failure 更改请求的标头:-

headers.put("Accept-Encoding", "gzip");

headers.put("Accept-Encoding", "identity");

是不是webclient因为任何原因无法处理gzip压缩的响应!!

提前致谢!!

【问题讨论】:

  • 是的。基本上我从 webclient 得到问题。问题基本上是如何在 webclient 中解决相同的 JSonParseException。
  • 你能手动解析payload吗?设置调试器或检查客户端发送的内容并尝试解析它。也尝试使用不同的客户端“postman, curl..etc”发送相同的发布请求
  • 是的,当端点在邮递员中发布时,它会给出 JSON 响应。
  • 可以使用类似这样的方式测试令牌是否有效:gist.github.com/thomasdarimont/46358bc8167fce059d83a1ebdb92b0e7

标签: spring-webclient jsonexception


【解决方案1】:

删除标头(“Accept-Encoding”、“gzip”) 您不会收到 JSON 解码错误:非法字符 ((CTRL-CHAR, code 31))

如果响应长度增加超过默认阈值,则响应将被压缩并给出 JSON 解码错误。

【讨论】:

    【解决方案2】:

    我错过了对响应对象实现 Serializable。

    【讨论】:

    • 我知道问题出在哪里了。
    【解决方案3】:

    当我将 spring-cloud-openfeign-core 的版本升级到 2.2.5.RELEASE 时,我已经开始使用 Spring boot 的 feign 依赖 spring-cloud-openfeign-core 得到这个异常。

    对于那些升级到2.2.5.RELEASE或更高版本的人,如果他们已经拥有feign.request.compression=true的属性,就会面临这个问题。

    这背后的技术原因是,在 FeignContentGzipEncodingAutoConfiguration 条件类 属性注释签名从 @ConditionalOnProperty("feign.compression.request.enabled", matchIfMissing = false)@ConditionalOnProperty(value = "feign.compression.request.enabled"),所以默认情况下 FeignContentGzipEncodingInterceptor 被触发。 GitHub reference

    解决方法

    如果您调用的 spring-boot 服务没有处理 compressed request 的机制,则使用以下属性禁用 feign 请求压缩

    feign.request.compression=false.
    

    注意:很遗憾,我们在 spring-boot 中仍然没有开箱即用的解决方案来处理压缩请求refer

    【讨论】:

      猜你喜欢
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 2011-06-18
      相关资源
      最近更新 更多