【问题标题】:JAXB -EclipseLink MOXy Object type not getting marshalled correctlyJAXB -EclipseLink MOXy 对象类型未正确编组
【发布时间】:2016-02-01 22:58:07
【问题描述】:
public class ResposneMessage {

    private int status;
    private String code = "";
    private String message = "";
    private Object data;
}

“DictType”未编组:

{
  "code": "",
  "data": "com.testapp.model.DictType@7c9a897 com.testapp.model.DictType@43581423 com.testapp.model.DictType@217adb02 com.testapp.model.DictType@6ff992bb com.testapp.model.DictType@253e12c3 com.testapp.model.DictType@2644f34b com.testapp.model.DictType@51919e4a com.testapp.model.DictType@72deb289 com.testapp.model.DictType@27231e1b com.testapp.model.DictType@26fc6f1f com.testapp.model.DictType@7b42c644 com.testapp.model.DictType@7c8f695f com.testapp.model.DictType@43637313",
  "message": "",
  "status": 200
}

默认不封送Object 类型。

【问题讨论】:

标签: jax-rs eclipselink moxy


【解决方案1】:

Object 无法编组为任何有意义的 JSON 表示,因为负责编组对象的库使用反射来执行此操作。它似乎只查看您定义的层次结构中最顶层的类,并编组在那里找到的任何内容。 由于Object 没有任何内容,因此您只需获得它的.toString() 表示。

FWIW:当您尝试编组接口或(抽象)超类时,也会发生同样的情况。您只能在编组输出中看到接口/超类本身定义的属性 - 无论后代类声明/定义什么。 举个例子:

public class SuperClass {
}

public class OtherClass extends SuperClass {
    public String someProperty = "test";
}

public class MarshallMe {
    public SuperClass classTest = new OtherClass();
}

这将编组为 "classTest":{},因为 SuperClass 没有自己的任何属性。

【讨论】:

  • 谢谢,但我怎样才能使用 Moxy 做到这一点。因为restful结果是Object,所以wen可以返回不同的结果。我们现在使用 Jackson 的 ObjectMapper 来生成 json。
猜你喜欢
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多