yhs0092 opened a new issue, #4378: URL: https://github.com/apache/servicecomb-java-chassis/issues/4378
## 问题现象 Java-Chassis 2.8.14 版本, 进行 RestTemplate 调用, 通过`getForObject`方法发送请求, 并指定返回值类型为一个自定义类型, 如下: ``` BizResp response = template.getForObject(url, BizResp.class); // BizResp 为用户自定义返回值类型 ``` `template.getForObject`实际返回的response类型为 `LinkedHashMap`, 导致上述代码报 ClassCastException 异常. ## 根因分析 Java-Chassis 的 RestTemplate调用流程里,使用者指定的返回值类型要生效,依赖于 `CseRequestCallback` 将返回值类型传递给 `CseClientHttpRequest`,再由 `CseClientHttpRequest#prepareInvocation` 方法传递到 `invocation.setSuccessResponseType` 方法中,再由 `DefaultHttpClientFilter#extractResponse` 方法调用 `invocation.findResponseType` 方法取出生效。 而在 `RestTemplate#getForObject` 方法流程中,Java-Chassis 不会创建 `CseRequestCallback` 对象,因此传递 response 类型信息的流程串不起来,`DefaultHttpClientFilter`中只能用 `Object.class` 做反序列化,拿到的是 `LinkedHashMap` 类型的 response body。 ## 规避措施 改用 RestTemplate 的 `exchange` 方法做 GET 调用, `CseRestTemplate` 的 `exchange` 方法中会调用`httpEntityCallback`方法, `httpEntityCallback`再创建`CseRequestCallback`实例, 可以将返回值类型信息传递流程串起来, 让使用者拿到预期类型的 response body. ## 期望 Java-Chassis 能够修复 `getForObject` 等方法, 使其支持用户指定返回值类型. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
