尝试将改造错误响应的errorBody()转换为ResponseWrapper,如下所示:
retrofit.responseBodyConverter<ResponseWrapper<R>>(Failure::class.java, arrayOf(object : Annotation {}))
.convert(response.errorBody())
ResponseWrapper如下:
sealed class ResponseWrapper<R> {
data class Success<R>(val payload: R) : ResponseWrapper<R>()
data class Failure<R>(
@field:Json(name = "code") val code: Int = INVALID_CODE,
@field:Json(name = "status") val status: String = "",
@field:Json(name = "exception") val exception: String = "",
@field:Json(name = "message") val message: String = "",
@field:Json(name = "fieldErrors") val fieldErrors: List<FieldError> = emptyList()
) : ResponseWrapper<R>()
class NotFound<R> : ResponseWrapper<R>()
class NoInternet<R> : ResponseWrapper<R>()
data class FieldError(
@field:Json(name = "field") val field: String,
@field:Json(name = "message") val message: String,
@field:Json(name = "rejectValue") val rejectValue: String
) }
通过Retrofit / Moshi这样做,将产生如下错误消息:
java.lang.AbstractMethodError: abstract method "java.lang.Class java.lang.annotation.Annotation.annotationType()"
at retrofit2.converter.moshi.MoshiConverterFactory.jsonAnnotations(MoshiConverterFactory.java:122)
at retrofit2.converter.moshi.MoshiConverterFactory.responseBodyConverter(MoshiConverterFactory.java:91)
at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:330)
at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:313)
我在这里错了什么?