【问题标题】:IllegalArgumentException: Unable to create @Body converter for classIllegalArgumentException:无法为类创建 @Body 转换器
【发布时间】:2021-11-22 13:42:32
【问题描述】:

每当我尝试使用 Retrofit 创建请求时,它都会失败。奇怪的是它之前工作过,我没有改变任何关于代码的东西。现在几个月后它不再起作用了。我收到以下错误:IllegalArgumentException: Unable to create @Body converter for class apis.Config (parameter #2)

我尝试从 Gson 更改为 Moshi,但并没有解决问题。我还搜索了 Stackoverflow,发现人们也有类似的问题,但解决方案似乎对我不起作用,因为它们主要是关于重复的 SerializedNames,而我的代码中并非如此。

API 服务

interface ApiService {

    @Headers(
        "Content-Type: application/json"
    )
    @POST("api")
    fun getActivityInvite(@Path("voiceChannelId") voiceChannelId: String, @Body body: Config, @Header("Authorization") authorization: String): Call<ActivityInvite>

}

配置类

data class Config(@SerializedName("target_application_id") val targetApplicationId: String){

    @SerializedName("max_age")
    val maxAge = 86400

    @SerializedName("max_uses")
    val maxUses = 0

    @SerializedName("target_type")
    val targetType = 2

    @SerializedName("temporary")
    val temporary = false

    @SerializedName("validate")
    val validate = null

}

API 类

class Api {

    private val retrofit: Retrofit = Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build()
    private val apiService: ApiService = retrofit.create(ApiService::class.java)
    private val props: Properties = PropertiesUtil.getProperties("project.properties")

    fun getActivityInvite(voiceId: String, activityId: String): String?{
        val config = Config(activityId)
        val request = apiService.getActivityInvite(voiceId, config, "Bot ${props.getProperty("bot_token")}")
        val response = request.execute()
        return response.body()?.getInviteUrl()
    }
}

【问题讨论】:

    标签: java json kotlin gson retrofit


    【解决方案1】:

    所以我找到了问题的解决方案。显然问题是由我使用 OpenJDK 16 某些带有反射的东西引起的。我在运行代码时通过使用 JVM 参数 --illegal-access=permit 解决了这个问题。该解决方案不是最佳的,但它正在发挥作用。

    【讨论】:

      猜你喜欢
      • 2019-07-25
      • 2016-06-23
      • 2021-06-12
      • 2020-03-12
      • 1970-01-01
      • 2021-05-13
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多