SpringBoot:简述如何使用RestTemplate进行接口请求

一、GET请求

1.1 getForObject(URI url, Class responseType)

@Override
public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException {
    RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
    HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger);
    return execute(url, HttpMethod.GET, requestCallback, responseExtractor);
}

1.2 getForObject(String url, Class responseType, Map<String, ?> uriVariables)

@Override
public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
    RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
    HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger);
    return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
}

1.3 getForObject(String url, Class responseType, Object… uriVariables)

@Override
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables) throws RestClientException {
    RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
    HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor<T>(responseType, getMessageConverters(), logger);
    return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
}

1.4 getForEntity(URI url, Class responseType)

@Override
public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException {
    RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
    ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
    return execute(url, HttpMethod.GET, requestCallback, responseExtractor);
}

1.5 getForEntity(String url, Class responseType, Map<String, ?> uriVariables)

@Override
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
    RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
    ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
    return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
}

1.6 getForEntity(String url, Class responseType, Object… uriVariables)

@Override
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException {
    RequestCallback requestCallback = acceptHeaderRequestCallback(responseType);
    ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
    return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
}

二、HEAD请求

2.1 headForHeaders(URI url)

@Override
public HttpHeaders headForHeaders(URI url) throws RestClientException {
    return execute(url, HttpMethod.HEAD, null, headersExtractor());
}

2.2 headForHeaders(String url, Map<String, ?> uriVariables)

@Override
public HttpHeaders headForHeaders(String url, Map<String, ?> uriVariables) throws RestClientException {
    return execute(url, HttpMethod.HEAD, null, headersExtractor(), uriVariables);
}

2.3 headForHeaders(String url, Object… uriVariables)

@Override
public HttpHeaders headForHeaders(String url, Object... uriVariables) throws RestClientException {
    return execute(url, HttpMethod.HEAD, null, headersExtractor(), uriVariables);
}

三、PUT请求

3.1 put(URI url, Object request)

@Override
public void put(URI url, Object request) throws RestClientException {
    RequestCallback requestCallback = httpEntityCallback(request);
    execute(url, HttpMethod.PUT, requestCallback, null);
}

3.2 put(String url, Object request, Map<String, ?> uriVariables)

@Override
public void put(String url, Object request, Map<String, ?> uriVariables) throws RestClientException {
    RequestCallback requestCallback = httpEntityCallback(request);
    execute(url, HttpMethod.PUT, requestCallback, null, uriVariables);
}

3.3 put(String url, Object request, Object… uriVariables)

@Override
public void put(String url, Object request, Object... uriVariables) throws RestClientException {
    RequestCallback requestCallback = httpEntityCallback(request);
    execute(url, HttpMethod.PUT, requestCallback, null, uriVariables);
}

待补充。。。。。

©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页