当前位置:网站首页>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-07-28 22:23:00 【m0_54861649】
背景: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就可以了。
边栏推荐
- Summary of wrong questions of software designers
- SQL实现将多行记录合并成一行
- Real time data warehouse: meituan reviews Flink's real-time data warehouse application sharing
- 迅为IMX6开发板QT系统创建AP热点基于RTL8723-交叉编译iptables
- Leetcode64. Minimum path sum
- EN 12101-8:2011烟雾和热量控制系统防烟挡板—CE认证
- Is the declarative code of compose so concise?
- curl (7) Failed connect to localhost8080; Connection refused
- Leetcode62. 不同路径
- 【MySQL系列】MySQL数据库基础
猜你喜欢

Leetcode64. 最小路径和

【C】 Reverse string (two recursive ideas)

Use pytoch to quickly train the network model

Eight performance analysis indicators of effective supply chain management (Part 1)

The failure rate is as high as 80%. How to correctly complete the strategic planning of digital transformation?

Android studio connects to MySQL and completes simple login and registration functions

【MySQL系列】MySQL数据库基础

VMware VCSA 7.0 Install

失败率高达80%,数字化转型如何正确完成战略规划?

基于 FPGA 实现数字时钟详细原理讲解及验证结果
随机推荐
Principle of meter skipping
[TA frost wolf _may- "hundred people plan"] art 2.2 model basis
有效供应链管理的八大绩效分析指标(上)
How NAT configures address translation
Web系统常见安全漏洞介绍及解决方案-CSRF攻击
Powercl batch creates and manages virtual switches
【MySQL 8】Generated Invisible Primary Keys(GIPK)
@Transactional 注解使用详解
基于 FPGA 实现数字时钟详细原理讲解及验证结果
Is the declarative code of compose so concise?
【TA-霜狼_may-《百人计划》】图形3.6 纹理压缩——包体瘦身术
After SAP Oracle replicates a new instance, the remote connection of the database reports an error ora-01031
Leetcode61. 旋转链表
Create AP hotspots for imx6 development board QT system based on rtl8723 cross compile iptables
1-7 解决类中方法的this指向问题
1-5 类式组件
With the help of rpa+lcap, the enterprise treasurer management can be upgraded digitally
Three years after graduation, write to you and me who may be confused [turn]
Add build dependency error
VMware VCSA 7.0 Install
