Retrofit2 可以改装将@Path参数从自定义类转换为基本类型
我有以下改装请求:Retrofit2 可以改装将@Path参数从自定义类转换为基本类型,retrofit2,Retrofit2,我有以下改装请求: @GET("event/{id}") suspend fun getEvent(@Path("id") eventId: Long): Response<Event> 是否可以将EventId类作为路径参数传递,并告诉改型如何将值转换为Long? 这是我想要的方法签名: @GET("event/{id}") suspend fun getEvent(@Path("id") eventId: EventId): Response<Event> @GE
@GET("event/{id}")
suspend fun getEvent(@Path("id") eventId: Long): Response<Event>
是否可以将EventId
类作为路径参数传递,并告诉改型如何将值转换为Long
?
这是我想要的方法签名:
@GET("event/{id}")
suspend fun getEvent(@Path("id") eventId: EventId): Response<Event>
@GET(“事件/{id}”)
suspend fun getEvent(@Path(“id”)eventId:eventId):响应
我找到的解决方案是在改装生成器中注册转换器工厂
:
class EventIdConverterFactory : Converter.Factory() {
override fun stringConverter(type: Type, annotations: Array<Annotation>, retrofit: Retrofit) =
if (type == EventId::class.java) {
Converter<Any, String> { (it as EventId).value.toString() }
} else {
super.stringConverter(type, annotations, retrofit)
}
}
class EventIdConverterFactory:Converter.Factory(){
替代stringConverter(类型:类型,注释:阵列,改装:改装)=
if(type==EventId::class.java){
转换器{(它作为EventId).value.toString()}
}否则{
super.stringConverter(类型、注释、改装)
}
}
class EventIdConverterFactory : Converter.Factory() {
override fun stringConverter(type: Type, annotations: Array<Annotation>, retrofit: Retrofit) =
if (type == EventId::class.java) {
Converter<Any, String> { (it as EventId).value.toString() }
} else {
super.stringConverter(type, annotations, retrofit)
}
}