我在Java中有两种几乎相同的方法。唯一的区别是它们具有不同的参数类型。他们使用泛型并返回输入参数的类型T。我如何摆脱重复的代码?这是我的两种方法。最终,它们都用不同的类型来调用Spring restTemplate.exchange()
。否则方法是相同的。
public <T> T send(Class<T> expectedClass) throws KenectRestClientException {
if (this.logRequest) log.info("Making request: " + this.toString());
HttpEntity<Object> entity = new HttpEntity<>(this.body, getTokenHeaders(this.token));
RestTemplate restTemplate = new RestTemplate();
String compiledUrl = UrlUtils.replacePathParamsInUrl(this.url, this.pathParams, this.uriEncode) + UrlUtils.compileRequestParamsToUrl(this.requestParams, this.uriEncode);
Object[] incompatibleStrings = extractIncompatibleStrings(compiledUrl);
try {
return restTemplate.exchange(compiledUrl, this.httpMethod, entity, expectedClass, incompatibleStrings).getBody();
} catch(RestClientResponseException e) {
log.warning(RestClientResponseException.class.getSimpleName() +
" url: " + url +
" status code: " + e.getRawStatusCode() +
" error body: " + e.getResponseBodyAsString() +
" stacktrace: " + ExceptionUtils.getStackTrace(e));
throw new KenectRestClientException("Failed to send request");
} catch(ResourceAccessException e) {
log.warning("Resource access exception " +
" url: " + url +
" stacktrace: " + ExceptionUtils.getStackTrace(e));
throw new KenectRestClientException("Failed to send request");
}
}
public <T> T send(ParameterizedTypeReference<T> parameterizedTypeReference) throws KenectRestClientException {
if (this.logRequest) log.info("Making request: " + this.toString());
HttpEntity<Object> entity = new HttpEntity<>(this.body, getTokenHeaders(this.token));
RestTemplate restTemplate = new RestTemplate();
String compiledUrl = UrlUtils.replacePathParamsInUrl(this.url, this.pathParams, this.uriEncode) + UrlUtils.compileRequestParamsToUrl(this.requestParams, this.uriEncode);
Object[] incompatibleStrings = extractIncompatibleStrings(compiledUrl);
try {
return restTemplate.exchange(compiledUrl, this.httpMethod, entity, parameterizedTypeReference, incompatibleStrings).getBody();
} catch(RestClientResponseException e) {
log.warning(RestClientResponseException.class.getSimpleName() +
" url: " + url +
" status code: " + e.getRawStatusCode() +
" error body: " + e.getResponseBodyAsString() +
" stacktrace: " + ExceptionUtils.getStackTrace(e));
throw new KenectRestClientException("Failed to send request");
} catch(ResourceAccessException e) {
log.warning("Resource access exception " +
" url: " + url +
" stacktrace: " + ExceptionUtils.getStackTrace(e));
throw new KenectRestClientException("Failed to send request");
}
}
答案 0 :(得分:0)
如何传递一个布尔标志,您基于此标志调用exchange()
传递Class<T>
或ParameterizedTypeReference<T>
?
public <T> T send(T neededType, boolean flag) throws KenectRestClientException {
if (this.logRequest) log.info("Making request: " + this.toString());
HttpEntity<Object> entity = new HttpEntity<>(this.body, getTokenHeaders(this.token));
RestTemplate restTemplate = new RestTemplate();
String compiledUrl = UrlUtils.replacePathParamsInUrl(this.url, this.pathParams, this.uriEncode) + UrlUtils.compileRequestParamsToUrl(this.requestParams, this.uriEncode);
Object[] incompatibleStrings = extractIncompatibleStrings(compiledUrl);
try {
if (flag) {
return restTemplate.exchange(compiledUrl, this.httpMethod, entity, neededType.getClass(), incompatibleStrings).getBody();
} else {
return restTemplate.exchange(compiledUrl, this.httpMethod, entity, ParameterizedTypeReference.forType(neededType.getClass()), incompatibleStrings).getBody();
}
} catch(RestClientResponseException e) {
log.warning(RestClientResponseException.class.getSimpleName() +
" url: " + url +
" status code: " + e.getRawStatusCode() +
" error body: " + e.getResponseBodyAsString() +
" stacktrace: " + ExceptionUtils.getStackTrace(e));
throw new KenectRestClientException("Failed to send request");
} catch(ResourceAccessException e) {
log.warning("Resource access exception " +
" url: " + url +
" stacktrace: " + ExceptionUtils.getStackTrace(e));
throw new KenectRestClientException("Failed to send request");
}
}