【问题标题】:Unable to invoke no-args constructor for interface java.io.Serializable无法为接口 java.io.Serializable 调用无参数构造函数
【发布时间】:2017-06-07 10:54:11
【问题描述】:

我正在尝试调用 json web 服务(在 Liferay 中),特别是上传文件的方法(add-file-entry),在请求的参数中有一个对象 ServiceContext(https://docs.liferay.com/portal/6.2/javadocs/src-html/com/liferay/portal/service/ServiceContext.html#line.69) ,我将此对象参数作为 JSON 字符串发送,并且 json Web 服务必须将 json 字符串反序列化为 ServiceContext 对象。 ServiceContext 有一个属性:

private Map<String, Serializable> _attributes;

当我在这张地图中添加一个条目时:

servicecontext.setAttribute("fileEntryTypeId", 0);

当 GSON 尝试反序列化 servicecontext 对象时:

    ServiceContext servicecontext = new Gson().fromJson(ServiceContext, ServiceContext.class);

他抛出异常:无法为接口 java.io.Serializable 调用无参数构造函数。

请帮忙

【问题讨论】:

    标签: java json web-services gson liferay


    【解决方案1】:

    问题是因为Serializable类没有预建没有任何参数,所以应该不能用于map

    private Map<String, Serializable> _attributes; //no
    

    如果你愿意,你可以试试这个技巧,它应该有效,但请告诉我

    public class Myclass implements java.io.Serializable {
       public Myclass (){}
    }
    

    之后

    private Map<String, Myclass > _attributes;
    

    我不确定它是否适合您,请尝试并告诉我

    【讨论】:

    • 感谢您的快速回答,但问题是我无法更改 ServiceContext 类,它是在我使用的 Liferay 的 jars 中定义的。
    • 你不需要这样做,因为通过实现 Serializable 类你可以把它放在 Serializable 的位置
    • 好地图“私人地图 _attributes;”是 ServiceContext 类的一个属性,所以我想如果我想做你的解决方案,我必须改变 ServiceContext 类,你不觉得吗?
    猜你喜欢
    • 1970-01-01
    • 2016-02-18
    • 2017-12-20
    • 2018-12-29
    • 2015-08-02
    • 2019-05-07
    • 2020-02-14
    • 2022-12-05
    • 2020-06-15
    相关资源
    最近更新 更多