ltm150895
08/21/2017, 12:10 AMjava.lang.IllegalArgumentException: @JsonAdapter value must be TypeAdapter or TypeAdapterFactory reference.
at com.google.gson.internal.bind.JsonAdapterAnnotationTypeAdapterFactory.getTypeAdapter(JsonAdapterAnnotationTypeAdapterFactory.java:64)
at com.google.gson.internal.bind.JsonAdapterAnnotationTypeAdapterFactory.create(JsonAdapterAnnotationTypeAdapterFactory.java:46)
at com.google.gson.Gson.getAdapter(Gson.java:359)
at com.google.gson.Gson.toJson(Gson.java:592)
at com.google.gson.Gson.toJson(Gson.java:579)
at com.google.gson.Gson.toJson(Gson.java:534)
at com.google.gson.Gson.toJson(Gson.java:514)
at Util.SparkUtilKt.prepare(SparkUtil.kt:50)
This is my data class:
@JsonAdapter(UserDeserializer::class)
data class User(
@SerializedName("username")
val userName: String,
@SerializedName("password")
val password: String,
@SerializedName("email")
val email: String
) : Serializable
class UserDeserializer : JsonDeserializer<User> {
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): User {
json ?: throw IllegalStateException("JSON does not exist")
typeOfT ?: throw IllegalStateException("Type does not exist")
context ?: throw IllegalStateException("DeserializationContext does not exist")
val jSon = json.asJsonObject
return User(
userName = jSon.get("username").safeString(context),
password = jSon.get("password").safeString(context),
email = jSon.get("email").safeString(context)
)
}
}
the code is triggered like this:
Spark.get("/user", { request, response ->
val user = User(
userName = "Username",
password = "pass",
email = "<mailto:test@mail.com|test@mail.com>"
)
response.prepare(200, user)
})
////// here is prepare
/**
* This function helps us format our responses
*/
fun Response.prepare(statusCode: Int, content: Any): String {
status(statusCode)
type("application/json")
body(gson.toJson(content))
return body()
}