【异常】JSON parse error: Illegal character ((CTRL-CHAR, code 31)): only regular white space ( , ,

时间:2025-03-22 08:46:45

  【异常】JSON parse error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, 

feign服务端 gzip编码压缩

feign客户端 不支持gzip解码 导致json转换失败

fegin默认的Client对响应流不支持对gzip后的字节流进行解析,所以在序列化成对象时会存在解析问题。如果一定要接收可以使用ResponseEntity来接收,这样feign就不会对其反序列化了。至于=true,=true配置的内容在FeignAcceptGzipEncodingInterceptor,FeignContentGzipEncodingInterceptor

spring已添加支持,SpringCloud版升级到Hoxton即可

/spring-cloud/spring-cloud-openfeign/pull/230

1
2
3

# 对于OkHttpClient以外的http客户端,可以启用默认的gzip解码器以UTF-8编码解码gzip响应
=true
=true

问题 

feign 调用的时候报这个异常

: JSON parse error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens; nested exception is : Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
 at [Source: ($NonClosingInputStream); line: 1, column: 2]

解决

http调用的时候爆出这个错,其实就是参数过大,超过当前设置的最大参数长度,参数超过了,肯能部分被截掉了,导致json字符串不完整,也就解析出问题了。要么减小参数的长度,要么将参数的允许长度设置大一点。如下调整好min-request-size即可

如果不够接着调大一点

# feign 配置
feign:
  compression:
    request:
      enabled: true
      min-request-size: 8192
    response:
      enabled: true

详细错误信息

: JSON parse error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens; nested exception is : Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
 at [Source: ($NonClosingInputStream); line: 1, column: 2]
        at .(:391)
        at .(:343)
        at (:185)
        at (:160)
        at (:133)
        at (:122)
        at (:179)
        at (:146)
        at (:117)
        at (:895)
        at (:808)
        at (:87)
        at (:1070)
        at (:963)
        at (:1006)
        at (:909)
        at (:681)
        at (:883)
        at (:764)
        at (:227)
        at (:162)
        at (:53)
        at (:189)
        at (:162)
        at (:100)
        at (:117)
        at (:189)
        at (:162)
        at (:93)
        at (:117)
        at (:189)
        at (:162)
        at (:96)
        at (:117)
        at (:189)
        at (:162)
        at (:201)
        at (:117)
        at (:189)
        at (:162)
        at (:197)
        at (:97)
        at (:541)
        at (:135)
        at (:92)
        at (:78)
        at (:360)
        at .http11.(:399)
        at (:65)
        at $(:890)
        at $(:1789)
        at (:49)
        at (:1191)
        at $(:659)
        at $(:61)
        at (:750)
Caused by: : Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
 at [Source: ($NonClosingInputStream); line: 1, column: 2]
        at ._constructError(:2391)
        at ._reportError(:735)
        at ._throwInvalidSpace(:713)
        at .UTF8StreamJsonParser._skipWSOrEnd(:3057)
        at .(:756)
        at ._initForReading(:4761)
        at ._readMapAndClose(:4667)
        at (:3682)
        at .(:380)
        ... 55 more