2013-07-12 40 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

相关问题