MongoDB にアクセスするために、Jongo で Kotlin を使用しています。Jongo は、Jackson を使用してオブジェクトをシリアライズ/デシリアライズし、MongoDB からオブジェクトを保存および読み取ります。Jackson-Kotlin モジュールを使用して、コンストラクターを使用して Kotlin データ クラスをシリアル化します。
以下は、正常にシリアル化されるデータクラスの例です。
data class Workflow (
@field:[MongoId MongoObjectId] @param:MongoId
var id: String? = null,
val name: String,
val states: Map<String, State>
)
デシリアライズに失敗する同様のクラスの例を次に示します。
data class Session (
@field:[MongoObjectId MongoId] @param:MongoId
var id: String? = null,
var status: CallStatus,
var currentState: String,
var context: MutableMap<String, Any?>,
val events: MutableMap<String, Event>
)
Jackson のデシリアライズが失敗するため、Jongo によって次の例外がスローされます。
org.jongo.marshall.MarshallingException: Unable to unmarshall result to class example.model.Session from content { "_id" : { "$oid" : "56c4976aceb2503bf3cd92c2"} , "status" : "Ongoing" , "currentState" : "Start" , "context" : { } , "events" : { }}
... bunch of stack trace entries ...
Caused by: java.lang.IllegalArgumentException: Argument #1 of constructor [constructor for example.model.Session, annotations: [null]] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator
次のように Session データ クラスに完全に注釈を付けると、機能します。
data class Session (
@field:[MongoObjectId MongoId] @param:MongoId
var id: String? = null,
@JsonProperty("status")
var status: CallStatus,
@JsonProperty("currentState")
var currentState: String,
@JsonProperty("context")
var context: MutableMap<String, Any?>,
@JsonProperty("events")
val events: MutableMap<String, Event>
}
私の質問は、なぜワークフローで機能するのですか? 完全に注釈が付けられていない場合に、Session データ クラスのアンマーシャリングが失敗する微妙な違いは何ですか?
編集
違いは、ワークフロー テスト ケースを Gradle から実行してテストしたことです。このテスト ケースでは、異なるバージョンの Kotlin を使用し、次に IDEA IDE から実行したセッション テスト ケースを使用しました。IDEA の Kotlin プラグインの更新により、IDEA がテスト ケースの実行に使用する Kotlin のバージョンも更新されましたが、これには気づきませんでした。これにより、Kotlin と Jackson-Kotlin ライブラリのバージョンが一致しなくなりました。以下の受け入れられた回答は、物事を再び機能させるために何を構成する必要があるかを指摘しました。