Error while extracting response for type [] and content type [],json返回值被解析为xml
在使用restTemplate请求restful接口时,在特定情况下总会将返回的json数据解析为xml数据然后处理,接着就会爆出标题中的错误:
Error while extracting response for type [] and content type [application/xml;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character '5' (code 53) in content after '<' (malformed start element?).
at [row,col {unknown-source}]: [1,15395]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected character '5' (code 53) in content after '<' (malformed start element?).
根据错误信息来看,似乎响应头标记了返回类型为[application/xml;charset=UTF-8],然而实际情况是所有的返回数据都是[application/json;charset=UTF-8]。
跟踪restTemplate源码,发现由new RestTemplate(httpRequestFactory())创建的实例会有7个converter:
继续跟踪restTemplate的exchange,当对response进行类型转换时,会迭代当前实例中所有的converter,然后选择一个支持当前类型的converter执行,使用canRead来判断:
此时就发现了问题,在特定情况下,响应头的contentType被读作了"application/xml",然而此时的真实数据仍然为json格式。所以将用于xml格式的converter删除,则迭代器会寻找下一个可执行的converter即MappingJackson2HttpMessageConverter。或者将二者顺序换一下,降低xml的优先级。
解决办法:
1:删除xml的转换器
@Beanpublic RestTemplate restTemplate() {RestTemplate template = new RestTemplate(httpRequestFactory());// 排除掉xml的解析converter,避免将json数据当做xml解析List<HttpMessageConverter<?>> collect = template.getMessageConverters().stream().filter(m -> !(m instanceof MappingJackson2XmlHttpMessageConverter)).collect(Collectors.toList());template.setMessageConverters(collect);return template;}
2:降低xml转换器优先级
@Beanpublic RestTemplate restTemplate() {RestTemplate template = new RestTemplate(httpRequestFactory());// 将xml解析的优先级调低int xml = 0, json = 0;List<HttpMessageConverter<?>> messageConverters = template.getMessageConverters();for (int i = 0; i < messageConverters.size(); i++) {HttpMessageConverter<?> h = messageConverters.get(i);if (h instanceof MappingJackson2XmlHttpMessageConverter) {xml = i;} else if (h instanceof MappingJackson2HttpMessageConverter) {json = i;}}Collections.swap(template.getMessageConverters(), xml, json);return template;}
Error while extracting response for type [] and content type [],json返回值被解析为xml相关推荐
- Error while extracting response for type [class xxx] and content type application/xml;charset=UTF-8
强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan [前言] 最近在用restTemplate进行一次http请求时发现了报错(Error whil ...
- feign.codec.DecodeException: Error while extracting response for type
报错信息 feign.codec.DecodeException: Error while extracting response for type [java.util.List<cn.xxx ...
- Feign - Error while extracting response for type [class java.lang.String]
定义Feign接口 @RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.G ...
- openfeign远程调用接口报错:feign.codec.DecodeException: Error while extracting response for type
错误信息: feign.codec.DecodeException: Error while extracting response for type [java.util.List<xxxx. ...
- feign.codec.DecodeException: Error while extracting response for type报错记录
今天在对一个老项目进行了微服务的拆分 用feign调用一个老服务的时候抛出了这样的异常 Error while extracting response for type [java.util.List ...
- Caused by: org.springframework.web.client.RestClientException: Error while extracting response for t
spring cloud alibaba 三方请求收到处理成功,但是自己接收失败 feign.codec.DecodeException: Error while extracting respons ...
- Maven报错解决:Element 'dependency' cannot have character [children], because the type's content type is
在用maven的时候遇到报错: Element 'dependency' cannot have character [children], because the type's content ty ...
- Media Type 媒体类型(MIME Type、Content Type)
媒体类型用于声明随之而来的数据的格式.又称:MIME类型.MIME Type.Content Type. 是一种用于在异构系统.分布式网络之间声明并传递信息主体格式的方法. 一般的,在文件系统中,我们 ...
- restTemplate http请求报错:no suitable HttpMessageConverter found for response type and content type
报错信息: org.springframework.web.client.UnknownContentTypeException: Could not extract response: no sui ...
最新文章
- python 车牌识别简单_ZKTeco 智能车牌识别 停车如此简单
- python学精通要多久-学习Python从入门到精通需要多长时间
- Android开发技术周报 Issue#81
- #51CTO学院四周年#其实、其实,我就是来吐槽的”
- 小学经历——我家有猪
- Node.js+Koa开发微信公众号个人笔记(一)准备工作 - ZhangCui - 博客园
- adadelta算法_对C++用户比较友好的机器学习算法库
- Qt工作笔记-右键删除视图中的Item【坐标映射】
- 中文字符存储 mysql_中文字符的存储
- 【动态规划】LeetCode 377. Combination Sum IV
- Zabbix监控系统深度实践
- HighCharts:设置坐标轴字体样式
- 作战军事环境仿真系统软件解决方案
- 杭电2079-选课时间(题目已修改,注意读题)
- 公司地址变更,税务变更,公司搬家了怎么变更企业信息
- 电子计算机奏出美妙的交响改为把字句,按要求改写下面的句子. 美妙的琴声陶醉了兄妹俩. 改为被字句: 改为把字句:...
- Linux LCD驱动程序
- HTTP状态码(HTTP Status codes)简介
- 将txt文件转换为m3u文件
- Win10 go install 命令 打包 Go项目
热门文章
- AndroidQ 分屏窗口尺寸计算 (WMS部分)
- 机器学习与算法(8)--局部加权学习算法(LWR)
- php怎么压缩文件夹,PHP压缩文件夹 php
- 如何测试服务器端口是否打开?
- 创新实践 | SaaS增长新趋势:产品驱动增长PLG(下)
- 自恋的人脑袋有啥不一样?| 自恋型人格特质和前额脑结构
- zabbix最新数据中,没有值 45653:20211111:181222.474 [Z3005] query failed: [1526] Unknown error 1526 [insert in
- PC端网页布局——世纪佳缘(二)页面初搭建
- Android 实现短信接收监听--(短信动态权限添加)
- OpenCV每日函数 thinning函数(细化)