2016-10-19 76 views
0

全部,如何讓FasterXMLJackson使用@JsonCreator mixin?

任何人都可以看到我的方式的錯誤?

我需要自定義的反序列化,從JSON,在下拉向導中工作。我跟着一些如何,但它不起作用。結果是我沒有可用的,一個默認的,無參數的構造函數。相反,我有一個工廠方法。我無法修改我試圖反序列化的類。我可以序列化爲JSON序列化&反序列化XML和。但是從JSON反序列化是阻塞。

我已經閱讀了所有我可以在SO和其他地方找到的東西,但沒有任何規定的作品。

我創建一個混合:

public class PatientMixin { 

    @JsonCreator 
    public static ForecastPatient createForecastForecastPatient() { 
     return ForecastFactory.eINSTANCE.createForecastPatient(); 
    } 
} 

在我的DW Application.run(......)我這樣做:

<code before>   
environment.getObjectMapper().addMixIn(ForecastPatient.class, PatientMixin.class); 
<code after> 

上面應該夠傑克遜找到我的密新和打電話給工廠。相反,我得到一個錯誤:

Can not construct instance of forecast.ForecastPatient, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information 

我會想我有東西映射正常,但顯然不是。此外,我正在閱讀的許多示例似乎都過時了。

在此先感謝

+0

是forecast.ForecastPatient一個抽象類? –

+0

不,它不是抽象的,它是一個接口,其中工廠方法返回一個實現。 ForecastPatient et.al.是由eclipse建模框架生成的。 –

+0

Jackson無法實例化一個接口,您應該告訴jackson您想要實例化的實現 –

回答

0

By default, Jackson tries to use the "default" constructor (one that takes no arguments), when creating value instances. But you can also choose to use another constructor, or a static factory method to create instance. To do this, you will need to use annotation @JsonCreator, and possibly @JsonProperty annotations to bind names to arguments

https://github.com/FasterXML/jackson-annotations#using-constructors-or-factory-methods

就是說ForecastFactory.createForecastPatient()應@JsonCreator被標註

+0

如果您的意思是將ForecastFactory修改爲: –

+0

f您的意思是修改ForecastFactory至此:@JsonCreator public ForecastPatient createForecastPatient()。不能這樣做,因爲ForecastFactory是生成的代碼。這就是磨擦。 –

+0

我認爲我的Mixin是正確的,問題出在傑克遜找到它。有趣的是,它記得我曾經做過這個工作。 –