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、フィールドおよびクラス パラメーターの注釈がコピーされる場所を制御するために使用されます。これは、このパッケージ内の 1 つまたは複数のメタアノテーションを使用して、アノテーション タイプまたはアノテーション クラスのいずれかにアノテーションを付けることによって行われます。

したがって、注釈はコンストラクターパラメーターに移動します。コンストラクターパラメーターだけでなく、ゲッターにも割り当てる必要があります。

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