dubbo icon indicating copy to clipboard operation
dubbo copied to clipboard

接口的Map对象有有个字段值是new Object(),用hessian序列化就报错了

Open wangjohnny opened this issue 10 years ago • 2 comments

dubbo版本:2.4.11 返回类定义: ` public class JsonMap extends HashMap<String, Object> { private static final long serialVersionUID = 1L;

/** 通知消息 */
private static final String MESSAGE = "message";

/** 状态码 */
private static final String CODE = "code";

/** 数据 */
private static final String DATA = "data";

public static JsonMap of(boolean success, String message, Object data) {
    return of(success ? "2000" : "5000", message, data);
}

public static JsonMap of(String code, String message, Object data) {
    JsonMap map = new JsonMap();
    map.setCode(code);
    map.setMessage(message);
    map.setData(data);
    return map;
}

public static JsonMap of(boolean success, String message) {
    return of(success, message, new Object());
}

public static JsonMap of(String code, String message) {
    return of(code, message, new Object());
}

public static JsonMap of(boolean success) {
    return of(success, "");
}

public static JsonMap of(String code) {
    return of(code, "");
}

public JsonMap() {
    super();
    this.setCode("2000");
    this.setData(new Object());
    this.setMessage("");
}

public Object getData() {
    return this.get(DATA);
}

public void setData(Object data) {
    this.put(DATA, data);
}

public String getCode() {
    Object code = this.get(CODE);
    return code != null ? code.toString() : "";
}

public void setCode(String code) {
    this.put(CODE, code);
}

public String getMessage() {
    Object message = this.get(MESSAGE);
    return message != null ? message.toString() : "";
}

public void setMessage(String message) {
    this.put(MESSAGE, message);
}

public boolean isSuccess() {
    return this.getCode().equals("2000");
}

} `

业务方法:

` public JsonMap valid(String brokerSid) { Broker broker = brokerService.selectByPrimaryKey(brokerSid);

    if (!broker.getValid()) {
        return JsonMap.of(false, "错误信息");
    }

    return JsonMap.of(true);
}

`

看了hessian的部分源代码,在BasicSerializer的writeObject方法最后看到了这些代码 case 12: case 13: default: throw new RuntimeException(this.code + " " + String.valueOf(obj.getClass())); 12代表number,13代表Object。难道是Object与Number类型的字段就不进行序列化吗?

com.alibaba.dubbo.remoting.RemotingException: Failed to send response: Response [id=310, version=2.0.0, status=20, event=false, error=null, result=RpcResult [result={code=2000, data=java.lang.Object@42e3fe0, message=}, exception=null]], cause: java.lang.RuntimeException: 13 class java.lang.Object java.lang.RuntimeException: 13 class java.lang.Object at com.alibaba.com.caucho.hessian.io.BasicSerializer.writeObject(BasicSerializer.java:282) at com.alibaba.com.caucho.hessian.io.Hessian2Output.writeObject(Hessian2Output.java:408) at com.alibaba.com.caucho.hessian.io.MapSerializer.writeObject(MapSerializer.java:100) at com.alibaba.com.caucho.hessian.io.Hessian2Output.writeObject(Hessian2Output.java:408) at com.alibaba.dubbo.common.serialize.support.hessian.Hessian2ObjectOutput.writeObject(Hessian2ObjectOutput.java:92) at com.alibaba.dubbo.rpc.protocol.dubbo.DubboCodec.encodeResponseData(DubboCodec.java:200) at com.alibaba.dubbo.remoting.exchange.codec.ExchangeCodec.encodeResponse(ExchangeCodec.java:273) at com.alibaba.dubbo.remoting.exchange.codec.ExchangeCodec.encode(ExchangeCodec.java:77) at com.alibaba.dubbo.rpc.protocol.dubbo.DubboCountCodec.encode(DubboCountCodec.java:41)

wangjohnny avatar Jul 05 '16 07:07 wangjohnny

Hessian 要求被序列化的对象实现Serializable接口,Object没有

junfengcode avatar Jul 05 '16 09:07 junfengcode

@junfengcode 谢谢啊,我这个错误太低级了

wangjohnny avatar Jul 06 '16 06:07 wangjohnny