强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan

【前言】

最近在用restTemplate进行一次http请求时发现了报错(Error while extracting response for type [class xxx] and content type application/xml;charset=UTF-8)经过一番尝试后最终解决问题,在此记录一下。

【问题及解决】

一、封装http调用的方法如下:

 public <T> ResponseEntity<T> postJson(String url, String requestJson, Class<T> responseType) {HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON_UTF8);HttpEntity<String> entity = new HttpEntity<>(requestJson, headers);ResponseEntity<T> responseEntity;System.out.println(requestJson);responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, responseType);return responseEntity;}

二、提供方为的出参和入参均为json格式;

三、单步进行调试发现报如下错:

四、在网上查了一些资料并进行了相关验证,对调用方法设置对返回值的格式为json即可解决,相应代码如下:

  public <T> ResponseEntity<T> postJson(String url, String requestJson, Class<T> responseType) {HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON_UTF8);//设置接收返回值的格式为jsonList<MediaType> mediaTypeList = new ArrayList<>();mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);headers.setAccept(mediaTypeList);HttpEntity<String> entity = new HttpEntity<>(requestJson, headers);ResponseEntity<T> responseEntity;System.out.println(requestJson);responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, responseType);return responseEntity;}

【小结】

在之前的项目中不用设置接收值的格式也是可以的,之前系统用的SpringBoot版本是1.5.x,现在的项目是将Spring Boot版本升级到2.x;在进行新项目的开发时建议使用比较新的稳定版本的框架,如果是迁移则建议沿用老版本,如果老版本代码向新框架迁移一定要进行全流程的测试,不然可能会有新老版本不兼容而导致的bug。

Error while extracting response for type [class xxx] and content type application/xml;charset=UTF-8相关推荐

  1. feign.codec.DecodeException: Error while extracting response for type

    报错信息 feign.codec.DecodeException: Error while extracting response for type [java.util.List<cn.xxx ...

  2. Error while extracting response for type [] and content type [],json返回值被解析为xml

    在使用restTemplate请求restful接口时,在特定情况下总会将返回的json数据解析为xml数据然后处理,接着就会爆出标题中的错误: Error while extracting resp ...

  3. Feign - Error while extracting response for type [class java.lang.String]

    定义Feign接口 @RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.G ...

  4. openfeign远程调用接口报错:feign.codec.DecodeException: Error while extracting response for type

    错误信息: feign.codec.DecodeException: Error while extracting response for type [java.util.List<xxxx. ...

  5. feign.codec.DecodeException: Error while extracting response for type报错记录

    今天在对一个老项目进行了微服务的拆分 用feign调用一个老服务的时候抛出了这样的异常 Error while extracting response for type [java.util.List ...

  6. Caused by: org.springframework.web.client.RestClientException: Error while extracting response for t

    spring cloud alibaba 三方请求收到处理成功,但是自己接收失败 feign.codec.DecodeException: Error while extracting respons ...

  7. Could not extract response: no suitable HttpMessageConverter found for content type [text/html]

    目录 报错信息 源码分析 解决方法 修改 mappingJackson2HttpMessageConverter 配置 继承 mappingJackson2HttpMessageConverter 实 ...

  8. Error while extracting response for type

    在实体类的Date类型字段上加注解即可 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = ...

  9. 记一次feign调用报错:feign.codec.DecodeException: Error while extracting response for type [java...

    弯路: 报错的内容就是feign被调用方返回的结果无法解析. 所以一直以为是被调用法的返回对象类型和调用方接收的对象类型不一致导致的解析失败,经检查字段完全一致: 又因为我们用了lombok,所以类上 ...

最新文章

  1. win8 C盘空间不足的几种解决方法
  2. [zz]GNU C __attribute__ 机制简介
  3. dbda.class.php 下载,DBDA
  4. TI基于MSP430F67641的电能表技术方案
  5. 网件rax40可以刷梅林_美国网件发布全系列Wi-Fi6家用无线路由器,部署未来家用产品市场...
  6. jquery checkbox 选中、改变状态、change 和 click 事件
  7. Adobe illustrator 调整画板大小 - 连载17
  8. 基于Spring Security的认证授权_认证原理_授权流程_Spring Security OAuth2.0认证授权---springcloud工作笔记126
  9. 随机邻域嵌入_[读综述] 图嵌入的应用
  10. 利用互斥对象实现线程同步的实例说明
  11. 谈谈Runtime类中的freeMemory,totalMemory,maxMemory等几个方法
  12. 幕享 for Mac(全平台全设备投屏共享软件)中文版
  13. vue生成静态html文件_是否可以将Vue.js模板编译为静态HTML和CSS文件?
  14. 【原创】字典攻击教务处(BurpSuite使用)
  15. 雷达、信号处理方向的理解
  16. eclispe/myeclipse中输入法的问题
  17. 定期报告系统服务器出错 1,做好Web服务器的日常维护必备常识
  18. MySQL数据库(1)~~一起学习数据库冲冲冲
  19. K3实现按虚拟件/组件发料
  20. 【Tools】P4V基础操作

热门文章

  1. 木纹标识lisp_LISP架构中一种新的移动性管理方案研究
  2. 计算机图标右击管理打不开,电脑图标打不开怎么办6
  3. 卷积、线性时不变系统、因果系统
  4. 关于pcl1.11.0无法找到 pcl/io/openni_grabber.h的解决办法
  5. 下一代图像压缩格式科普---HEIF 与AVIF格式
  6. ADODB.Recordset 错误 #x27;800a0bb9#x27; 参数类型不正确,或不在可以接受的范围之内,或与其他参数冲突。
  7. 阿里飞冰使用教程-前端低代码可视化GUI速开发
  8. grpc双向流 python_gRPC Golang/Python使用
  9. 第四章创业者与创业团队
  10. 窗——开了又关,关了又开(改编)