kotlinx.serialization.SerializationException:找不到类“MultiPartFormDataContent”的序列化程序
Posted
技术标签:
【中文标题】kotlinx.serialization.SerializationException:找不到类“MultiPartFormDataContent”的序列化程序【英文标题】:kotlinx.serialization.SerializationException: Serializer for class 'MultiPartFormDataContent' is not found 【发布时间】:2021-04-25 18:56:53 【问题描述】:我正在尝试上传多个文件。
val ktorVersion = "1.5.0"
val serializationVersion = "1.0.1"
我就是这样做的:
override suspend fun uploadFiles(
binaryFiles: Map<String,ByteArray>
): BaseResponse<List<String>>
return client.submitForm
url(fileUploadUrl)
method = HttpMethod.Post
body = MultiPartFormDataContent(
formData
headers
append("Content-Type", "application/json")
append("Authorization", "Bearer $token")
binaryFiles.entries.forEach
append(
key = "files",
value = it.value,
headers = Headers.build
append(HttpHeaders.ContentDisposition, "filename=$it.key")
)
)
但是会抛出异常
kotlinx.serialization.SerializationException: Serializer for class 'MultiPartFormDataContent' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:91)
at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:130)
at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
at io.ktor.client.features.json.serializer.KotlinxSerializerKt.buildSerializer(KotlinxSerializer.kt:82)
at io.ktor.client.features.json.serializer.KotlinxSerializerKt.access$buildSerializer(KotlinxSerializer.kt:1)
at io.ktor.client.features.json.serializer.KotlinxSerializer.writeContent$ktor_client_serialization(KotlinxSe
at io.ktor.client.features.json.serializer.KotlinxSerializer.write(KotlinxSerializer.kt:26)
at io.ktor.client.features.json.JsonFeature$Feature$install$1.invokeSuspend(JsonFeature.kt:150)
at io.ktor.client.features.json.JsonFeature$Feature$install$1.invoke(JsonFeature.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:123)
at io.ktor.client.features.HttpCallValidator$Companion$install$1.invokeSuspend(HttpCallValidator.kt:106)
at io.ktor.client.features.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
at io.ktor.client.features.HttpRequestLifecycle$Feature$install$1.invokeSuspend(HttpRequestLifecycle.kt:37)
at io.ktor.client.features.HttpRequestLifecycle$Feature$install$1.invoke(HttpRequestLifecycle.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:133)
at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:79)
at io.ktor.client.HttpClient.execute(HttpClient.kt:187)
at io.ktor.client.statement.HttpStatement.executeUnsafe(HttpStatement.kt:104)
at com.example.package.core.data.network.services.upload.FileUploadApiImpl.uploadFiles(FileUploadApi.kt:99)
我也尝试过,但遇到了同样的问题:
suspend fun uploadFilesTest(
binaryFiles: Map<String,ByteArray>
): BaseResponse<List<String>>
return client.post(fileUploadUrl)
headers
append("Content-Type", ContentType.Application.Json)
append("Authorization", "Bearer $token")
body = MultiPartFormDataContent(
formData
binaryFiles.entries.forEach
this.appendInput(
key = "files",
size = it.value.size.toLong(),
headers = Headers.build
append(HttpHeaders.ContentDisposition, "filename=$it.key")
,
) buildPacket writeFully(it.value)
)
【问题讨论】:
【参考方案1】:看看你上次说你有同样问题的sn-p,你好像在这样的标题中指定内容类型
append("Content-Type", ContentType.Application.Json)
但您还希望它是多部分的,方法是将 body 设置为 MultiPartFormDataContent
,它还需要定义为标头 content-type:multipart/form-data; ...
,因此可能是此冲突导致了问题。
我实际上只是在我的代码中自己尝试过,这对我来说似乎是个问题。
【讨论】:
从标题中删除内容类型解决了问题【参考方案2】:我为 android 做了这个,但它也可能适用于 ktor。首先,将此行添加到构建项目 gradle 中的 dependencies 中:
classpath "org.jetbrains.kotlin:kotlin-serialization:1.5.21"
将这些也添加到构建应用程序 gradle:
plugins
...
id 'kotlinx-serialization'
dependencies
...
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2'
【讨论】:
以上是关于kotlinx.serialization.SerializationException:找不到类“MultiPartFormDataContent”的序列化程序的主要内容,如果未能解决你的问题,请参考以下文章