org.springframework.core.codec.DecodingException:JSON解码错误:非法字符((CTRL-CHAR,代码31))

时间:2020-08-27 03:29:59

标签: spring-webclient jsonexception

我在通过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: Ȁ

我曾尝试根据失败更改请求的标头:-

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

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

网络客户端是否由于某种原因无法处理压缩后的响应!

提前谢谢!

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

当我将spring-cloud-openfeign-core的版本升级为spring-cloud-openfeign-core时,我已经开始使用Spring Boot的假冒依赖项2.2.5.RELEASE来获取此异常。

对于那些升级到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.request.compression=false.

注意:不幸的是,我们在Spring Boot中仍没有现成的解决方案来处理压缩的请求refer

相关问题