2015-11-02 51 views
0
public class ResposneMessage { 

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

的 「DictType」 不封:JAXB -EclipseLink莫西對象類型沒有得到正確整理

{ 
    "code": "", 
    "data": "[email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]", 
    "message": "", 
    "status": 200 
} 

默認是不封送Object類型。

+0

這是重複的 - 請參閱這裏 - http://stackoverflow.com/questions/818327/jaxb-how-should-i-marshal-complex-nested-data-structures – kinshuk4

+0

@ kinshuk4除非那根本不是重複的但是完全不同的問題。 – PureSpider

回答

0

無法將Object編組爲任何有意義的JSON表示,因爲負責編組對象的庫使用反射來執行此操作。它似乎只查看您定義的層次結構中最頂級的類,並收集它在其中找到的任何內容。 由於Object沒有任何內容,因此您只需要獲得.toString()表示形式。

FWIW:當您嘗試編組接口或(抽象)超類時發生同樣的情況。您只能在編組輸出中看到interface/superclass自身定義的屬性 - 無論子類聲明/定義什麼。 拿這個例子:

public class SuperClass { 
} 

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

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

這馬歇爾剛剛"classTest":{},因爲SuperClass沒有任何自己的屬性。

+0

謝謝,但我怎樣才能使用Moxy做到這一點。因爲寧靜的結果是Object,所以可以返回不同的結果。我們現在使用Jackson的ObjectMapper來生成json。 –

相關問題