两种具有不同参数类型的相同方法

问题描述 投票:0回答:1

我在Java中有两个几乎相同的方法。唯一的区别是它们有不同的参数类型。它们使用泛型并返回输入参数的类型T.我怎样才能摆脱重复的代码?这是我的两种方法。最终他们都称不同类型的春天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");
    }
}
java generics
1个回答
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");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.