Kotlin,改造 2:lang.IllegalArgumentException:参数类型不得包含类型变量或通配符:java.util.Map<java.lang.String, ?>

a_s*_*ber 9 kotlin retrofit2

在我的 Kotlin, Retrofit 2 项目中:

class TransportService {

    // static analog
    companion object {
        @JvmStatic
        fun checkoutSessions(bodyMap: Map<String, Any>, callback: Callback<Void>) {
            val stripeMonitorRestClient = RestClientFactory.createRestClient(StripeRestClient::class.java!!)
            val headerMap = mutableMapOf<String, String>()
            headerMap["Accept"] = "application/json"
            headerMap["Content-Type"] = "application/x-www-form-urlencoded"
            headerMap["Stripe-Version"] = "2019-03-14; checkout_sessions_beta=v1"
            headerMap["Authorization"] = "Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc"
            val adMessagesList = stripeMonitorRestClient.checkoutSessions(headerMap, bodyMap) // error here 
            // asynchronously
            adMessagesList.enqueue(callback)
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

和休息客户端api:

import retrofit2.Call
import retrofit2.http.FieldMap
import retrofit2.http.HeaderMap
import retrofit2.http.POST
import retrofit2.http.FormUrlEncoded


interface StripeRestClient {

    @FormUrlEncoded
    @POST("checkout/sessions")
    fun checkoutSessions(@HeaderMap headers: Map<String, String>, @FieldMap body: Map<String, Any>): Call<Void>
}
Run Code Online (Sandbox Code Playgroud)

但我收到运行时错误

Exception in thread "main" java.lang.IllegalArgumentException: Parameter type must not include a type variable or wildcard: java.util.Map<java.lang.String, ?> (parameter #2)
Run Code Online (Sandbox Code Playgroud)

在这一行:

val adMessagesList = stripeMonitorRestClient.checkoutSessions(headerMap, bodyMap) 
Run Code Online (Sandbox Code Playgroud)

这里记录:

Hello, World! Current date(groovy) = Mon Apr 01 17:32:47 EEST 2019
Exception in thread "main" java.lang.IllegalArgumentException: Parameter type must not include a type variable or wildcard: java.util.Map<java.lang.String, ?> (parameter #2)
    for method StripeRestClient.checkoutSessions
    at retrofit2.Utils.methodError(Utils.java:52)
    at retrofit2.Utils.methodError(Utils.java:42)
    at retrofit2.Utils.parameterError(Utils.java:61)
    at retrofit2.RequestFactory$Builder.validateResolvableType(RequestFactory.java:720)
    at retrofit2.RequestFactory$Builder.parseParameterAnnotation(RequestFactory.java:551)
    at retrofit2.RequestFactory$Builder.parseParameter(RequestFactory.java:295)
    at retrofit2.RequestFactory$Builder.build(RequestFactory.java:182)
    at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:65)
    at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:25)
    at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:168)
    at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
    at com.sun.proxy.$Proxy0.checkoutSessions(Unknown Source)
    at myproject.service.TransportService$Companion.checkoutSessions(TransportService.kt:19)
    at myproject.MainKt.main(Main.kt:14)
Run Code Online (Sandbox Code Playgroud)

Sai*_*jib 11

只需在接口方法之前使用 @JvmSuppressWildcards 注释 -

喜欢 -

 @POST("add_new_purchase.php")
 @JvmSuppressWildcards
 fun addNewPurchase(@Body purchaseList: List<PurchaseModel>):  Call< BaseResponse>
Run Code Online (Sandbox Code Playgroud)

  • 更多详情可以参考 https://github.com/square/retrofit/issues/3275 和 https://github.com/square/retrofit/issues/1805 (5认同)

Kir*_*ehe 1

我在开始迁移到 Kotlin 时遇到了同样的问题。如果StripeRestClient是在 Java 中,调用就可以工作。

尝试将接口转换为Java。