【发布时间】:2019-12-27 06:49:56
【问题描述】:
我正在将现有代码迁移到超类/子类情况。见下文。
父类中的方法:
public <T> ResponseEntity<T> makeRequest(HttpMethod method, String endpoint, HttpEntity<?> request, Class<T> responseType) {
//DO stuff here
}
在子类中有对上述 makeRequest() 的调用。
ParameterizedTypeReference<List<MyOtherClass>> typeRef = new ParameterizedTypeReference<List<MyOtherClass>>() {};
ResponseEntity<List<MyOtherClass>> response = makeRequest(HttpMethod.GET,uriComponents.toUriString(), request, typeRef);
但 最后一个参数 似乎导致签名不匹配。 Eclipse 是这么说的。
“ParentWS 类型中的方法 makeRequest(HttpMethod, String, HttpEntity, Class) 不适用于参数 (HttpMethod, String, HttpEntity, ParameterizedTypeReference>)”
我不明白为什么,父方法签名说任何类型的任何类。所有其他子类都只是简单地使用 String.class 调用父方法,就像这样。
response = makeRequest(HttpMethod.GET, healthCheckUrlEndpoint, request, String.class);
但为什么 ParameterizedTypeReference>() {} 会导致问题? 我以前从未使用过 ParameterizedTypeReference,它对我来说是全新的。请指教。
【问题讨论】:
-
简单地说,
ParameterizedTypeReference不是Class。你打算通过什么Class?是List<MyOtherClass>吗?如果是这样,为什么不直接通过呢?
标签: java spring jakarta-ee