当前位置:网站首页>feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
2022-08-02 02:25:00 【m0_67392661】
背景:feign调用不通,报错,困了我快一个月!!!可恶,中间花了两天解决,把百度都试了一遍,都没有用,都快哭了,现在说下解决过程。
①开始我打开了日志进行进一步查看报错信息,需要在feign使用配置文件中加入日志级别,日志级别是FULL,并在配置文件中也加入日志级别。


②然后发现报错404 not found,开始以为是参数不够,被调用为三个参数,调用时传两个参数,加上了之后还是报错,然后发现是注解用错了,之前用的是@PathVariable(复制过来没有改),使用@RequestParam后错误改变(低级错误)。
③报错:nested exception is com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space ( , , ) is allowed between tokens
at [Source: (ByteArrayInputStream); line: 1, column: 2]。

④然后feign调用接口,使用String接收,把字符串打印出来,发现乱码了???

还发现传回来是二进制数据??
然后开始了疯狂的百度中,中间试了无数次,各种加什么配置,去掉压缩之类的都不行,还有加过滤器什么的也不行。
最后终于在一个大哥的文章中发现了答案,他和我一样是打印出来发现乱码,这个问题就是gzip接收到的还是压缩数据,所以乱码,还是试了一下他说没用的配置,结果对我有用!!!!!(他下面真正对她有用的方法对我没用,也是加拦截器)
答案:在配置文件中加入feign.compression.response.useGzipDecoder为true即可。
⑤乱码的事情没了,发现只有中文乱码了,靠!!!!!!又整了我好久好久,百度的所有方案都试过了,还看了feign日志所有参数,好像也没问题的,人都快哭了,终于找到了解决方案。
@Configuration
public class FeignConfig implements RequestInterceptor {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
@Bean
public Decoder feignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder( () -> {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter() {
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
Object result=null;
if ("java.lang.String".equals(type.getTypeName())) {
result= StreamUtils.copyToString(inputMessage.getBody(), Charset.forName("utf8"));
} else {
result=super.read(type, contextClass, inputMessage);
}
return result;
}
};
return new HttpMessageConverters(fastConverter);
}));
}
@Override
public void apply(RequestTemplate requestTemplate) {
try {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
if (null != servletRequestAttributes) {
HttpServletRequest request = servletRequestAttributes.getRequest();
// 获取所有头文件信息的key
Enumeration<String> headerNames = request.getHeaderNames();
if (null != headerNames) {
while (headerNames.hasMoreElements()) {
// 获取头文件的key和value
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
// 跳过content-length,不然可能会报too many bites written问题
if ("content-length".equalsIgnoreCase(headerName)) {
continue;
}
// 将令牌数据添加到头文件中,当用feign调用的时候,会传递给下一个微服务
requestTemplate.header(headerName, headerValue);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
参考:Feign乱码 - 知乎
答案:加了个解码器,转成utf8就可以了。
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
- Speed up your programs with bitwise operations
- 20. 用两个栈实现队列
- Project Background Technology Express
- BI - SQL 丨 WHILE
- Can Youxuan database import wrongly be restored?
- AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
- Handwritten Blog Platform ~ Day Two
- The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
- C language inserted into the characters of simple exercises
猜你喜欢

Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒

FOFAHUB usage test

BI-SQL丨WHILE

openGauss切换后state状态显示不对

记一次gorm事务及调试解决mysql死锁

【web】理解 Cookie 和 Session 机制

MySQL8 download, start, configure, verify

Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案

AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
![[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games](/img/8a/07ca69c6dcc22757156cb615e241f8.png)
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
随机推荐
Oracle19c安装图文教程
2022 NPDP take an examination of how the results?How to query?
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
What to study after the PMP exam?The soft exam ahead is waiting for you~
60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
Centos7 安装postgresql并开启远程访问
BI-SQL丨WHILE
菜刀webshell特征分析
使用docker安装mysql
MySQL8 download, start, configure, verify
2022河南青训联赛第(三)场
Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
ros多客户端请求服务
Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
个人博客系统项目测试
Rasa 3 x learning series - Rasa - 4873 dispatcher Issues. Utter_message study notes
53. 最小的k个数
51. 数字排列
