簡體   English   中英

兩種具有不同參數類型的相同方法

[英]Two identical methods with different parameter types

我在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");
    }
}

傳遞一個布爾標志怎么樣,你可以調用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");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM