【发布时间】:2019-07-23 02:21:45
【问题描述】:
我正在尝试实现一个处理外部 API 调用的基本功能:
inline fun <reified T> get(url: String): T? {
try {
val restTemplate = RestTemplate()
val response = restTemplate.exchange<Any>(
url,
HttpMethod.GET,
headersForRestTemplate,
T::class)
return response.getBody() as T
} catch (e: Exception) {
log.info("Exception ::" + e.message)
throw ServiceException(e)
}
}
我的称呼很简单:
api.get<SWObject>(Utils.SW_API)
当尝试执行该代码时,我得到一个强制转换异常:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to jp.co.xx.demo.models.SWObject
返回的对象不是SWObject 类的实例,而是LinkedHashMap。我仍在为reified 或inline 关键字苦苦挣扎,如果我的实现没有遵循最佳实践,请见谅。
【问题讨论】:
标签: spring-boot generics kotlin resttemplate