2013-07-12 106 views
2
class AgentResponse[T] @JsonCreator()(@JsonProperty("result") val result: T, @JsonProperty("status") val status: ResponseStatus) 

class ResponseStatus @JsonCreator()(@JsonProperty("succeeded") val succeeded: Boolean, @JsonProperty("message") val message: String, @JsonProperty("timeStamp") val timeStamp: Long) 

new ObjectMapper().registerModule(DefaultScalaModule).writer().writeValue(out, new AgentResponse(result, new ResponseStatus(true, "OK", now))) 

它拋出錯誤:如何使用Jackson對多參數構造函數序列化scala對象?

JsonMappingException: No serializer found for class com.fg.mail.smtp.rest.Handler$AgentResponse and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS)) 

斯卡拉對象如何看起來應該像爲它達到預期效果?

回答

3

scala.annotation.meta引用:

缺省情況下,(val - ,var - 或平原)註釋構造參數結束的參數,而不是任何其他實體。字段上的註釋默認只會在字段上結束。

程序包scala.annotation.meta中的元註釋用於控制複製字段和類參數的註釋。這是通過使用此包中的一個或多個元註釋來註釋註釋類型或註釋類來完成的。

因此,註釋轉到構造函數參數。您需要將其分配給getter以及構造函數參數:

class MyClass @JsonCreator() (@(JsonProperty @getter @param) val arg: String) 
+0

感謝Peter,您當場了。 – lisak

+0

@Sloin請讓我知道它是否正常工作。我沒有時間去測試它。 –

+0

是的,它現在按預期工作,謝謝 – lisak

相關問題