Skip to content
Advertisement

IllegalArgumentException: Unable to create @Body converter for class

Whenever I try to create a request with Retrofit it fails. The strange thing is that it worked before, I didn’t change anything about the code. Now months later it doesn’t work anymore. I’m getting the following error: IllegalArgumentException: Unable to create @Body converter for class apis.Config (parameter #2)

I tried changing from Gson to Moshi but it didn’t solve the problem. I also searched Stackoverflow and saw that people had similar problems but the solutions didn’t seem to work for me as they were mostly about duplicate SerializedNames which isn’t the case in my code.

API Service

interface ApiService {

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

}

Config Class

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

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()
    }
}

Advertisement

Answer

So I found the solution to the problem. Apparently the problem is caused by me using OpenJDK 16 certain things with reflection. I solved the issue by using the JVM parameters --illegal-access=permit when running the code. The solution is not optimal but it’s working.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement