0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Kotlin(Quarkus)で外部APIのレスポンスをJacksonでバインドするとエラーになる

Posted at

はじめに

Quarkusで外部APIを叩いたあとに返ってきたJsonをバインドするのに困ったのでまとめます

問題

以下のようなAPIを叩くDriverを用意しました

Driver.kt
    fun find(request: Map<String, Any?>): TodoJson =
        ApiClient.find(
                request["id"]?.toString() ?: null,
            )
}

interface ApiClient {
    @GET
    @Path("/v1/todo")
    fun getfind(@QueryParam("id") id: String): TodoJson
}

data class TodoJson(val name: String, val done: Boolean)

ここで返却する値はモックサーバーを用意して以下のようにしました

"response": {
    "status": 200,
    "jsonBody": {
      "todo": {
        name: "test",
        done: true
      }
    }
 }

しかしなぜかバインドがうまくいきませんでした
カラム名や型もただしく謎でした

解決方法

モックサーバーのヘッダーにContent-Type: "application/json"を入れることで解決できました
これをJacksonでは認識してバインドしてくれているようです

"response": {
    "status": 200,
    "jsonBody": {
      "todo": {
        name: "test",
        done: true
      }
    },
    "headers": {
      "Content-Type": "application/json"
    }
 }

おわりに

すでに動いていたテストデータを確認して気づくことができました
デバックも難しいので気づけてよかったです

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?