Feign请求响应结果被截取com.fasterxml.jackson.core.io.JsonEOFException

Stella981
• 阅读 1161

在生产环境使用feign调用外部接口时,偶尔会出现下面错误

2020-10-15 11:00:18,535 [] ERROR com.shein.abc.rmp.controller.RecExplainConfigController - rec_explain_query.fail
f
feign.codec.DecodeException: Error while extracting response for type [class com.alibaba.fastjson.JSONObject] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected end-of-input in field name; nested exception is com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input in field name
 
 at [Source: (PushbackInputStream); line: 1, column: 108927]
 
        at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:169)
 
        at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:133)
 
        at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76)
 
Caused by: org.springframework.web.client.RestClientException: Error while extracting response for type [class com.alibaba.fastjson.JSONObject] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected end-of-input in field name; nested exception is com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input in field name
 
 at [Source: (PushbackInputStream); line: 1, column: 108927]
 
        at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:115)
 
        at org.springframework.cloud.openfeign.support.SpringDecoder.decode(SpringDecoder.java:60)
 
        at org.springframework.cloud.openfeign.support.ResponseEntityDecoder.decode(ResponseEntityDecoder.java:45)
 
        at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:165)
 
        ... 112 common frames omitted
C
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected end-of-input in field name; nested exception is com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input in field name
 
 at [Source: (PushbackInputStream); line: 1, column: 108927]
 
        at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:243)
 
        at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:225)
 
        at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:100)
 
        ... 115 common frames omitted
C
Caused by: com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input in field name
 
 at [Source: (PushbackInputStream); line: 1, column: 108927]
 
        at com.fasterxml.jackson.core.base.ParserMinimalBase._reportInvalidEOF(ParserMinimalBase.java:618)
 
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.parseEscapedName(UTF8StreamJsonParser.java:1962)
 
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.slowParseName(UTF8StreamJsonParser.java:1867)
 
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._parseName(UTF8StreamJsonParser.java:1651)
 
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextFieldName(UTF8StreamJsonParser.java:1005)
 
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:896)
 
        at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:654)
        ... 117 common frames omitted

这个很明显是json反序列化失败,根据提示json字符串不全,根据上面的报错,分别预估了几个可能原因,并针对性的排查。 1、feign中是否有拦截器,截取了响应结果 排查结果:无

2、是否服务端响应的结果就不全 经过排查,服务端响应结果没问题,并支持gzip压缩

3、打印feign的debug日志,并查询出header中的content-length是否与响应结果不一致,这里不了解content-length的可以参考下面链接: Content-Length是如何工作的 HTTP 协议中的 Transfer-Encoding 这两篇文章中有详细的描述,当实际响应结果比content-length大时会被截取

既然服务端支持gzip压缩,且请求结果返回值比较大,这时我们是否考虑feign开启gzip压缩

feign:
  compression:
    request:
      # 开启请求压缩
      enabled: true
      # 配置压缩的 MIME TYPE
      mime-types: text/xml,application/xml,application/json
      # 配置压缩数据大小的下限
      min-request-size: 2048
    response:
      # 开启响应压缩
      enabled: true

开启压缩后重新请求,报下面的错误

feign.codec.DecodeException: Error while extracting response for type [class com.alibaba.fastjson.JSONObject] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse 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: (PushbackInputStream); line: 1, column: 2]
    at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:169)
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:133)
Caused by: org.springframework.web.client.RestClientException: Error while extracting response for type [class com.alibaba.fastjson.JSONObject] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse 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: (PushbackInputStream); line: 1, column: 2]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:115)
    at org.springframework.cloud.openfeign.support.SpringDecoder.decode(SpringDecoder.java:60)
    at org.springframework.cloud.openfeign.support.ResponseEntityDecoder.decode(ResponseEntityDecoder.java:45)
    at feign.SynchronousMethodHandler.decode(SynchronousMethodHandler.java:165)
    ... 114 common frames omitted
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse 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: (PushbackInputStream); line: 1, column: 2]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:243)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:225)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:100)
    ... 117 common frames omitted
Caused by: com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
 at [Source: (PushbackInputStream); line: 1, column: 2]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:693)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._throwInvalidSpace(ParserMinimalBase.java:644)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._skipWSOrEnd(UTF8StreamJsonParser.java:2951)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:691)
    at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4142)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3085)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:237)
    ... 119 common frames omitted

这个报错比较明显了,反序列化解析出问题,经过排查发现是HttpURLConnection不支持gzip反序列化,SpringCloud需要升级到Hoxton才能解决该问题:

https://github.com/spring-cloud/spring-cloud-openfeign/pull/230

还有一种方案是将httpclient换成okhttp,如下配置变更:

feign:
  okhttp:
    enabled: true
  httpclient:
    enabled: false

pom中增加相关依赖

        <!-- okhttp -->
        <dependency>
            <groupid>io.github.openfeign</groupid>
            <artifactid>feign-okhttp</artifactid>
        </dependency>

配置后重新启动,不再报错。

参考链接:

https://www.jianshu.com/p/df37eb5f2169

https://blog.csdn.net/wo18237095579/article/details/83344529

https://imququ.com/post/transfer-encoding-header-in-http.html

https://blog.piaoruiqing.com/2019/09/08/do-you-know-content-length/

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Easter79 Easter79
3年前
springcloud(八) Hystrix监控
一、Feign项目Hystrix自带的监控  在feign项目pom.xml添加:<!1,使用Hystrix的模块hystrixmetricseventstream,就可将这些监控的指标信息以text/eventstream的格式暴露给外部系统。spring
Stella981 Stella981
3年前
Spring Cloud之Feign 转发请求头(header参数)
在做接口请求时,我们经常会在header头中增加一些鉴权信息,如token或jwt,那么在通过fegin从Aserver去调用Bserver的接口时,如果Bserver的接口需要header信息,我们需要将Asever获取的header转发到B上。解决方式我们需要实现Feign提供的一个接口RequestInterc
Easter79 Easter79
3年前
SpringCloud Alibaba微服务实战二十
!(https://oscimg.oschina.net/oscnet/d8c498ec1a794ad3ac37338f40db0851.png)在之前的项目中我们已经实现了使用Feign调用远程接口,本章内容主要是借助sentinel实现Feign接口熔断器功能。概述首先我们看看不使用熔断器的情况下调用一个没有启动的服务会出现
Stella981 Stella981
3年前
OpenJDK11与Spring Cloud Finchley的不兼容问题与解决
本文的环境:OpenJDK11.0.4,SpringCloudfinchleySR4,SpringBoot2.0.3最近遇到了一个问题,在feign调用的时候,时常会出现这样一个奇怪的错误:2019100708:00:00.620ERRORxxx,e1ba4c7540954aa3,871b99c4576d42e3
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
JavaScript常用函数
1\.字符串长度截取functioncutstr(str,len){vartemp,icount0,patrn/^\x00\xff/,strre"";for(vari
Stella981 Stella981
3年前
Spring Cloud微服务架构从入门到会用(三)—服务间调用Feign
微服务最重要的一个功能是服务间调用,各个服务互相依赖。比如电商系统有订单服务,有库存服务。在我们购买一件商品的时候,需要生成订单和减库存。这里我们就要用到服务间调用Feign。Feign是一个http请求调用的轻量级框架,可以以Java接口注解的方式调用Http请求,而不用像Java中通过封装HTTP请求报文的方式直接调用。接下来我们新建两个modu
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这