zoukankan      html  css  js  c++  java
  • 记一次序列化的JSON解析问题

    一、问题初探

    我今天在使用Ribbon 远程调用的时候

    消费者去消费服务端所提供的服务,在使用Post请求中的PostForEntity传递参数的时候,我是这样写的

    Ribbon 消费端:

    User 是一个 Pojo 对象,代码如下:

    public class User {
    
        private Integer id;
        private String name;
    
        public User(Integer id, String name) {
            this.id = id;
            this.name = name;
        }
    
        get... and set ...
    }
    

    Ribbon 服务端:

    就是一个很简单的Ribbon 远程调用的一个操作,报如下错误:

    跟入源码:

    然后就不明觉厉了,第一开始比较烦躁,一直以为是消费端的问题,因为错误是从消费服务端报出来的。后来经过冷静发现,上述代码是在处理服务端响应的时候报出来的,应该还是服务端报错来的错误。

    2019-04-14 13:56:27.183  WARN 3500 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not construct instance of com.service.provider.pojo.User: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.service.provider.pojo.User: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
     at [Source: java.io.PushbackInputStream@25979c35; line: 1, column: 2]
    

    代码并不能看懂什么意思,求助google,从stackoverflow上找到了问题的答案:

    没有加默认的构造函数?????? 这是什么鬼? 默认的构造函数和JSON解析有什么关系?

    但是能够解决问题了,我检查了一遍我一直以为我的值没有传递过去,但我看已经传递到服务端了(避免了一个白痴问题)。

    二、问题原因

    华丽的分割线

    事实印证了我是个菜比。

    事情是这样的

    Ribbon的消费者向服务者提供的数据转换是要把对象转换为二进制数据,这个过程称之为序列化

    而服务端收到二进制的数据再转换为对应的对象,这个过程称之为反序列化

    序列化会把对象保存在磁盘中,或允许直接在网络中直接传输对象

    反序列化就是收到二进制的数据,再转换为对应的对象。

    而实现对象序列化的方法有两种:

    1. 实现序列化接口

    2. 提供无参构造函数

    而上面报错的原因就是因为没有提供默认的构造函数或者实现序列化接口!!!!!

    三、解决问题

    添加无参构造函数

    解决问题一,再上面的Pojo类上添加默认的构造函数

    public class User {
    
        private Integer id;
        private String name;
    
        public User(){}
        public User(Integer id, String name) {
            this.id = id;
            this.name = name;
        }
    
        get and set...
    }
    

    再次调用接口,客户端能够打印出来对应的日志

  • 相关阅读:
    C#单例模式的三种写法
    【TFS】增加组员,以及用户权限分配
    Mongodb实用网址记录
    关于JS 对象与JSON对象
    多线程操作
    检测到有潜在危险的 Request.Form 值
    JavaScript 【正则表达式验证数字代码】
    【SQL】大杂烩
    IE 中创建 子窗口 传值 与接收值 【window.showModalDialog】
    UITabbarView Tabbar
  • 原文地址:https://www.cnblogs.com/cxuanBlog/p/10944164.html
Copyright © 2011-2022 走看看