杰克逊-无法从START_OBJECT令牌

时间:2019-05-13 10:35:37

标签: kotlin jackson data-class

我正在尝试通过HTTP请求从我的背部获取一些数据,并将其映射为模型的MutableList。

在2种情况下,代码完全相同,但是其中一种不起作用,并且我第一次遇到错误?我在Google上进行了搜索,但是所有代码示例与我的代码示例确实都不同,因此解决方案也是如此。

这是我遇到的错误:

D/OkHttp: Callback failure for call to http://10.0.2.2:5000/...
D/OkHttp: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
D/OkHttp:  at [Source: (String)"{"recurrenceId":1,"meetingId":4,"weekDay":21}"; line: 1, column: 1]
D/OkHttp:     at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
D/OkHttp:     at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1139)
D/OkHttp:     at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1093)
D/OkHttp:     at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:332)
D/OkHttp:     at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:265)
D/OkHttp:     at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
D/OkHttp:     at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
D/OkHttp:     at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3023)
D/OkHttp:     at fr.intech.neith.api.RecurrenceApi.toDo(RecurrenceApi.kt:34)
        at fr.intech.neith.helpers.ApiHelper$GET$1.onResponse(ApiHelper.kt:35)
D/OkHttp:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
D/OkHttp:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
D/OkHttp:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)

这是我的数据类:

package fr.intech.neith.models

data class Recurrence(val recurrenceId: Int = 0,
                      val meetingId: Int = 0,
                      val weekDay: Array<Int> = arrayOf())

这是我发出GET请求的API:

    package fr.intech.neith.api

import...

class RecurrenceApi(context: Context): IOnApiResponse {
    var itodo: IToDoRecurrenceList? = null
    override fun toDo(body: String) {
        val mapper = ObjectMapper()

        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        //val reader: ObjectReader = mapper.reader().forType(Meeting.class);
        val reader = mapper.readValue<MutableList<Recurrence>>(body)
        itodo!!.toExecuteR(reader)
    }

    var client = OkHttpClient()
    var api = ApiHelper(client, context)
    var url: String = "http://10.0.2.2:5000" + "/api/recurrence" // 192.168.43.135 4G - 10.0.2.2 ÉMULATEUR

    fun getRecurrenceList(meetingId: Int, cb: IToDoRecurrenceList) {
        val url2: String = url + "/${meetingId}";
        itodo = cb
        api.GET(url2, this)
    }
}

这里有什么问题吗?

这是一个工作示例,与第一个完全相似,因为它的工作方式相同:

package fr.intech.neith.models
import java.io.Serializable
import java.util.*

data class Meeting(val hostFirstName: String = "",
                   val hostLastName: String = "",
                   val estimatedTime: String = "",
                   val startDate: Date = Date(),
                   val meetingId: Int = 0,
                   val name: String = "",
                   val hasEnded: Boolean = false,
                   val hostId: Int = 0,
                   val recurrence: Array<Int> = arrayOf(),
                   val index: Int = 0,
                   val isRecurrent: Boolean = false): Serializable

package fr.intech.neith.api


import android.content.Context
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import fr.intech.neith.IOnApiResponse
import fr.intech.neith.IToDoList
import fr.intech.neith.helpers.*
import fr.intech.neith.models.Meeting
import okhttp3.OkHttpClient

class MeetingApi(context: Context): IOnApiResponse {
    var itodo: IToDoList<Meeting>? = null
    override fun toDo(body: String) {
        val mapper = ObjectMapper()

        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        val reader = mapper.readValue<MutableList<Meeting>>(body)
        itodo!!.toExecute(reader)
    }

    var client = OkHttpClient()
    var api = ApiHelper(client, context)
    var url: String = "http://10.0.2.2:5000" + "/api/meeting" // 192.168.43.135 4G - 10.0.2.2 ÉMULATEUR

    fun getMeetingList(userId: Int, cb: IToDoList<Meeting>) {
        val url2: String = url + "/${userId}/names"
        itodo = cb
        api.GET(url2, this)
    }
}

感谢您的回答。

编辑:这已解决了问题:mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)

1 个答案:

答案 0 :(得分:0)

来自https://craftsmen.nl/kotlin-create-rest-services-using-jersey-and-jackson/

  

为了反序列化Kotlin数据类,我们必须在ObjectMapper实例上注册Jackson的KotlinModule。

https://github.com/FasterXML/jackson-module-kotlin

  

增加对Kotlin类和数据类的序列化/反序列化支持的模块。以前,必须在Kotlin对象上存在默认构造函数,以便Jackson可以反序列化到该对象中。通过此模块,可以自动使用单个构造函数类,并且还支持具有辅助构造函数或静态工厂的类。