你好,我正在尝试将一个通用方法作为控制器基本方法实现,但我无法理解的问题出现在通用方法签名中。
<T> ResponseEntity<T> makeApiCall(String path, HttpMethod httpMethod, T body, boolean isAdmin){
String sender = isAdmin ? adminHash : userHash;
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", sender);
headers.add("Content-Type", "application/json");
HttpEntity<T> entity = new HttpEntity<>(body,headers);
ResponseEntity<T> responseEntity = restTemplate.exchange(path, HttpMethod.POST, entity, body.getClass());
return responseEntity;
}
我目前遇到的编译错误如下:
Incompatible equality constraint: T and capture of ? extends Object
getClass()
结果分配给Class<T>
变量呢? - Lew BlochT
必须是可具体化的类型。否则,如果T
是一个参数化类型,比如Foo<Bar>
,那么你就必须提供一个Class<Foo<Bar>>
,但这并不存在于安全范畴内。 - newacct