• om.alibaba.com.caucho.hessian.io.HessianProtocolException: com.alibaba.com.caucho.hessian.io.ObjectDeserializer: unexpected object java.lang.String (CU) 调用dubbo返回这个错误


    方法一:

     本人为调用dubbo 接口,返回一个HashMap<Object,List<Student>>对象, 在反序列化map的时候出现问题,

     解决方法为 把返回的对象封装成对象 再返回

    方法二: 人家提供的一个方法

    传输的数据为

    @Data
    public class ResultBean implements Serializable {
        private static final long serialVersionUID = 7316712281088805027L;
        /**
         * 返回标识 正常返回为正数 异常为负数
         */
        private Integer code;
        /**
         * 返回文字描述
         */
        private String message;
        /**
         * 返回数据
         */
        private Serializable result;
    
        public ResultBean() {
        }
    
        public ResultBean(Integer code, String message) {
            this.code = code;
            this.message = message;
        }
    
        public ResultBean(Integer code, String message, Serializable result) {
            this.code = code;
            this.message = message;
            this.result = result;
        }
    }
    
    

    dubbo provider 代码

        @Override
        public ResultBean resultBean() {
            HashMap<String, Object> map = new HashMap<>();
            map.put("username", "xiaoyong");
            return new ResultBean(1, "hello world", map);
        }
    

    dubbo consumer 调用resultBean 时 会报一下错误

    Caused by: com.alibaba.com.caucho.hessian.io.HessianProtocolException: com.alibaba.com.caucho.hessian.io.ObjectDeserializer: unexpected object java.lang.String (username)
        at com.alibaba.com.caucho.hessian.io.AbstractDeserializer.error(AbstractDeserializer.java:131)
        at com.alibaba.com.caucho.hessian.io.AbstractDeserializer.readMap(AbstractDeserializer.java:111)
        at com.alibaba.com.caucho.hessian.io.AbstractDeserializer.readMap(AbstractDeserializer.java:119)
        at com.alibaba.com.caucho.hessian.io.Hessian2Input.readObject(Hessian2Input.java:2093)
        at com.alibaba.com.caucho.hessian.io.Hessian2Input.readObject(Hessian2Input.java:2074)
        at com.alibaba.com.caucho.hessian.io.JavaDeserializer$ObjectFieldDeserializer.deserialize(JavaDeserializer.java:406)
    

    原因是 ObjectDeserializer这个类没有实现readMap方法 解决方法 重写 ObjectDeserializer

    package com.alibaba.com.caucho.hessian.io;
    
    import java.io.IOException;
    
    public class ObjectDeserializer extends AbstractDeserializer {
        private Class _cl;
        private MapDeserializer mapDeserializer; // 新加的代码
    
        public ObjectDeserializer(Class cl) {
            _cl = cl;
            mapDeserializer = new MapDeserializer(cl); // 新加的代码
        }
    
        /** 新加的代码 begin */
        @Override
        public Object readMap(AbstractHessianInput in) throws IOException {
            return readMap(in, null, null);
        }
    
        @Override
        public Object readMap(AbstractHessianInput in, Class<?> expectKeyType, Class<?> expectValueType) throws IOException {
            return this.mapDeserializer.readMap(in, expectKeyType, expectValueType);
        }
        /** 新加的代码 end */
    
        @Override
        public Class getType() {
            return _cl;
        }
    
        @Override
        public Object readObject(AbstractHessianInput in) throws IOException {
            return in.readObject();
        }
    
        @Override
        public Object readObject(AbstractHessianInput in, String[] fieldNames) throws IOException {
            throw new UnsupportedOperationException(String.valueOf(this));
        }
    
        @Override
        public Object readList(AbstractHessianInput in, int length) throws IOException {
            throw new UnsupportedOperationException(String.valueOf(this));
        }
    
        @Override
        public Object readLengthList(AbstractHessianInput in, int length) throws IOException {
            throw new UnsupportedOperationException(String.valueOf(this));
        }
    
        @Override
        public String toString() {
            return getClass().getSimpleName() + "[" + _cl + "]";


    参考链接:https://www.jianshu.com/p/9ac316fae3ae

  • 相关阅读:
    JavaScript完整总结
    vue引入iframe的父子页面的数据传递
    随笔开发中笔记
    关于表格(table)的操作
    es6--6.字符串相关
    ES6--5.数组4个新增方法
    ES6--4.解构赋值
    sublime 远程连接服务器编辑
    ajaxSubmit
    修改 debian 时区
  • 原文地址:https://www.cnblogs.com/redBack/p/11737853.html
Copyright © 2020-2023  润新知