Unable to create converter for xxx.NetworkResponse<Auth> for method AuthService

BUG解决要点

  • @POST + @Body 必须写

  • NetworkResponse<T> 必须是 @Serializable data class

  • Auth 必须 @Serializable

  • 不要用 sealed / Any

哪怕 Auth 是 @Serializable,只要 NetworkResponse 有下面任意一条,就一定失败:

  • ❌ 没有 @Serializable
  • ❌ 是 sealed class
  • ❌ 是 open class
  • ❌ 有 Any / Map<String, Any>
  • ❌ 泛型 T 没有限定
  • ❌ 有自定义构造逻辑

Retrofit 会在创建接口代理时直接抛这个异常。

gradle(非常关键,很多人漏)

js 复制代码
dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
    implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0")
}
js 复制代码
plugins {
    id("org.jetbrains.kotlin.plugin.serialization")
}

代码示例

如果是Body的JSON请求,必须添加**@Body**

kotlin 复制代码
interface AuthService {

    @POST("login/password")
    suspend fun loginByPassword(
        @Body params: Map<String, String>
    ): NetworkResponse<Auth>
}
kotlin 复制代码
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class NetworkResponse<T>(
    @SerialName("code")
    val code: Int,

    @SerialName("message")
    val message: String,

    @SerialName("data")
    val data: T? = null
)
kotlin 复制代码
import kotlinx.serialization.Serializable

@Serializable
data class Auth(
    val token: String,
    val refreshToken: String,
    val expire: Long
)

根因是 kotlinx.serialization 无法为 NetworkResponse 生成 Serializer

相关推荐
小满zs6 小时前
Go语言第三章(五谷轮回)
后端·go
qq_589666058 小时前
TypeScript 完整入门教程
前端·javascript·typescript
霸道流氓气质10 小时前
SpringBoot中通用工具类库(Utils)封装与使用实践
spring boot·后端·python
用户0595401744611 小时前
LLM对话记忆测试踩坑实录:手工回归30分钟,自动化后2分钟发现3个隐藏Bug
前端·css
Ashley的成长之路13 小时前
前端性能优化实战手册·第2篇:资源加载策略全解
前端·性能优化·资源加载·http/3·性能优化实战·资源加载优化
浅水壁虎13 小时前
vue基础(第二章 )
前端·javascript·vue.js
界面开发小八哥13 小时前
界面控件DevExtreme v26.1新版亮点——支持Angular 22
前端·javascript·angular.js·devexpress·ui开发·devextreme
Getflare13 小时前
前端 + UI 设计 + AI:这不是三个工种,是一个新三角能力模型(附自检清单)
前端·人工智能·ui
oil欧哟13 小时前
我做了一个 Vibe Coding 术语学习站:VibeHub
前端·ai·agent·独立开发·vibe coding