背景:使用restTemplate推送数据给第三方时,ResponseEntity内部反序列化失败。

@Slf4j
@Component
public class MessagePush {@Value("${xxxx.pushUrl}")private String pushUrl;@Autowiredprivate RestTemplate restTemplate;public void push(String msg) {log.info("监听到数据:" + msg);Map<String, String> params = new HashMap<>();params.put("apiKey", "22222");params.put("apiSecret", "1111");params.put("content", msg);HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON_UTF8);HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(params, headers);ResponseEntity<Result> responseEntity = restTemplate.postForEntity(URI.create(pushUrl), httpEntity, Result.class);log.info("responseEntity对象为:{}", responseEntity);if (responseEntity.getStatusCodeValue() == 200 && responseEntity.getBody().getCode().equals(0)) {log.info("成功推送数据:{}", msg);} else {log.error("发送失败,失败原因:{}", responseEntity.getBody().getMessage());}}@Datapublic class Result { // 一开始定义的是非静态内部类private Integer code;private String message;private Object data;}}

报错信息:

Error while extracting response for type
[class com.demo.third.MessagePush$Result] and content type [application/json;charset=UTF-8];
nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
Cannot construct instance of `com.demo.third.MessagePush$Result`
(although at least one Creator exists): can only instantiate non-static inner class by using default, no-argument constructor;
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.demo.third.MessagePush$Result` (although at least one Creator exists):
can only instantiate non-static inner class by using default, no-argument constructor
at [Source: (PushbackInputStream); line: 2, column: 3]

解决:因为定义的Result是内部类,在反序列化时没有外部对象的实例,所以无法完成实例化,必须是静态内部类,才能在该类的内部直接new Result();方式来对内部类进行实例化,所以将Result类定义为静态的即可。

JSON反序列化失败:although at least one Creator exists): can only instantiate non-static inner class by usi相关推荐

  1. 反序列化失败although at least one Creator existsno delegate- or property-based Creator 异常

    标题所指问题也可能是 @Builder @NoArgsConstructor 冲突 以上问题出现的原因是 Entity上加上 @Builder ,会默认为类加上全参构造函数,且提供以建造器模式构造对象 ...

  2. localdatetime转化为date_为啥你用@JsonFormat注解时,LocalDateTime会反序列化失败?

    写在前面 最近,有个小伙伴问我:我在SpringBoot项目中,使用@JsonFormat注解标注LocalDateTime类型的字段时,LocalDateTime反序列化失败,这个我该怎么处理呢?别 ...

  3. Spring Security + Spring Session Redis——【SecurityContext】和【AuthenticationToken】JSON反序列化问题解决方案

    前置 Spring Session + Redis--自定义JSON序列化解决方案 问题描述 Spring Security + Spring Session使用中Redis,默认序列化方式是JdkS ...

  4. Spring Boot分布式系统实践【扩展1】shiro+redis实现session共享、simplesession反序列化失败的问题定位及反思改进...

    前言 调试之前请先关闭Favicon配置 spring:favicon:enabled: false 不然会发现有2个请求(如果用nginx+ 浏览器调试的话) 序列化工具类[fastjson版本1. ...

  5. JSON parse error: Cannot construct instance of “xxx“(although at least one Creator exists)

    今天写SpringBoot出现一个错误 JSON parse error: Cannot construct instance of `priv.kuki.param.AddressListParam ...

  6. Json反序列化与Java泛型

    Java的JSON库有很多,本文分析google的Gson和alibaba的fastjson,在Java泛型场景反序列化的一些有意思的行为.考虑下面的json字符串: ["214748364 ...

  7. Newtonsoft.Json反序列化(Deserialize)出错:Bad JSON escape sequence

    使用Newtonsoft.Json反序列化收到的字串为JObject或其它支持的数据模型,有时错误,提示如下: Bad JSON escape sequence: \c. Path 'idno', l ...

  8. Java中含有泛型的 JSON 反序列化问题

     作者:明明如月学长 blog.csdn.net/w605283073/article/details/107350113 一.背景 今天无聊之园提了一个问题,涉及的示例大致如下: public st ...

  9. json校验失败的原因

    如下原因会造成JSON校验失败,而且会让你不知道为什么失败 JSON字符串里的非数字型键值没有双引号 JSON中存在\t这样的制表符,看起来和空格一样,但是就是因为它的存在校验不通过.去掉就能过了. ...

最新文章

  1. 在VMware Workstation上安装Ubuntu 16.04 Server操作系统
  2. 旧式计算机,西雅图计算机博物馆 “复活”1973年老式计算机
  3. Pandas图表自定义数据格式
  4. MOSS Farm Architecture
  5. 常见MIME类型例表
  6. Servlet工作原理解析2(以Tomcat为例)
  7. 软件基本功:开发测试中的穷举归纳法
  8. OpenStack Rocky Octavia 的实现与分析(零)架构简介
  9. 山东新动能软件创新·创业大赛 首场宣讲答疑会顺利举行
  10. HTML5CSS3网页设计仿微信通讯录页
  11. WebSphere-解决办法:UndefinedVariableException: Undefined variable HOST
  12. 小米2017校园招聘笔试题
  13. 魔兽世界8.0哪个服务器稳定,魔兽世界活得最安逸的BOSS!8.0版本才拿到7.0服务器首杀!...
  14. 华为OD机试(21-40)老题库解析Java源码系列连载ing
  15. 基于C语言设计的俄罗斯方块小游戏(VS2017运行)
  16. UC浏览器首页滑动动画实现
  17. 计算机屏幕关机按钮没有了怎么办,电脑每次关机图标不见了怎么办啊
  18. 2016年6月被举报钓鱼网站前10 假冒建行最多
  19. 基于 RISC-V SoC 的可配置 FFT 系统设计(2)RISC-V 的起源及其发展历程
  20. PPG衬衫改变了什么

热门文章

  1. js逆向JSVMP篇新版某乎_x-zes-96算法还原
  2. 字节、字、bit、bite的关系
  3. 《LKD3粗读笔记》(6)内核数据结构
  4. 魔百盒 修改时间服务器,魔百盒网关服务器下发超时
  5. 细数AJAX应用程序开发的七宗罪
  6. 8086系统中 BHE# 和 A0 引脚
  7. 这是一个神奇(神器)的网站
  8. 国内黑客组织及代表性人物
  9. 韩国国会表决通过中韩自贸协定批准案
  10. 《史蒂夫·乔布斯传》官方正式中文版电子书