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)))

它抛出错误:

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) )

scala 对象应该如何按预期工作?

4

1 回答 1

3

引用scala.annotation.meta

默认情况下,(val- ,var- 或普通)构造函数参数上的注释最终在参数上,而不是在任何其他实体上。默认情况下,字段上的注释仅在字段上结束。

包中的元注释scala.annotation.meta用于控制字段和类参数的注释被复制到哪里。这是通过使用此包中的一个或多个元注释来注释注释类型或注释类来完成的。

所以注释转到构造函数参数。您需要将它分配给 getter 以及构造函数参数:

class MyClass @JsonCreator() (@(JsonProperty @getter @param) val arg: String)
于 2013-07-12T14:38:42.247 回答