2014-01-21 44 views
1

我正在嘗試使用Swagger作爲我的Scala基於來自wordnik的swagger-play2播放2.2項目。在處理Joda DateTime時似乎有問題。使用wordnik樣片播放項目,在「訂單」案例類,Swagger播放框架2.2 - Joda日期時間問題

@ApiModel("Order") 
case class Order (
    @(ApiModelProperty @field)(position=1)id: Long, 
    @(ApiModelProperty @field)(position=2)petId: Long, 
    @(ApiModelProperty @field)(position=3)quantity: Int, 
    @(ApiModelProperty @field)(position=4)shipDate: DateTime, 
    @(ApiModelProperty @field)(position=5)status: String 
) 

我改變了SHIPDATE從Java日期喬達DateTime和它拋出,當我來電/店/訂單API錯誤:

[JsonMappingException: Class org.joda.time.tz.CachedDateTimeZone has no default constructor; 
can not instantiate default bean value to support 'properties=JsonSerialize.Inclusion.NON_DEFAULT' annotation 
(through reference chain: scala.collection.convert.SeqWrapper[0]->models.Order["shipDate"]->org.joda.time.DateTime["zone"])] 

任何想法?

謝謝。

回答

2

我有一個類似的問題。或者說,我確實得到了同樣的例外。 因此,我會發布適用於我的解決方案。

解決方案是調整使用ScalaJsonUtil.mapper的BaseApiController.toJsonString(data:Option [Any])方法。

更換

ScalaJsonUtil.mapper.writeValueAsString(d) 

val m = ScalaJsonUtil.mapper 
    m.setSerializationInclusion(JsonInclude.Include.NON_EMPTY) 
    m.writeValueAsString(d) 

JsonInclude.Include.NON_EMPTY是從以下導入

import com.fasterxml.jackson.annotation.JsonInclude 
1

或者您可以使用此LIB https://github.com/iheartradio/play-swagger

這個庫採用與註解不同的方法(這迫使您學習新的API),您可以直接在您的路徑文件中將swagger spec作爲註釋寫入。它會自動爲joda.datetime生成定義。