一、RestTemplate配置
使用RestTemplate進行服務調用時,需要進行一些配置,才能使RestTemplate按照我們想要的方式進行調用。
我們通常將RestTemplate的配置封裝在一個類中,以便於管理和維護,該類通常命名為RestTemplateConfig。
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public HttpClient httpClient() {
return HttpClientBuilder.create().build();
}
@Bean
public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory(HttpClient httpClient) {
return new HttpComponentsClientHttpRequestFactory(httpClient);
}
@Bean
public RestTemplate httpsRestTemplate(HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory) {
return new RestTemplate(httpComponentsClientHttpRequestFactory);
}
}
上述代碼中的RestTemplate bean會被Spring進行內部化處理,在項目中可以直接通過@Autowired註解進行注入。
HttpClient的實例化採用了builder模式,它與我們平時使用的HttpClient實例化方式不同,使用了一些新的配置選項來支持更多的請求形式。
HttpComponentsClientHttpRequestFactory的實例化使用了HttpClient實例,以處理請求。
最後,我們通過實例化httpsRestTemplate方法來配置RestTemplate的行為,並將HttpComponentsClientHttpRequestFactory傳遞給它以便於使用我們的HttpClient對象來處理請求。
二、RestTemplate使用
使用RestTemplate進行服務調用大概分為兩種情況:
1.直接使用RestTemplate進行調用
在不涉及到https和其他特定協議的服務調用時,我們可以直接使用RestTemplate進行調用。
@Autowired
private RestTemplate restTemplate;
public String callService() {
String url = "http://example.com/service";
String response = restTemplate.getForObject(url, String.class);
return response;
}
上述代碼中,我們直接使用RestTemplate的getForObject方法進行調用,並將調用結果轉換為字符串。
2.使用RestTemplate進行https服務調用
在進行https服務調用時,我們需要先配置HttpClient和SSL相關的配置,然後再通過RestTemplate進行調用。
在RestTemplate的配置中,我們定義了一個httpsRestTemplate bean,通過該bean我們可以進行https服務調用。
@Autowired
private RestTemplate httpsRestTemplate;
public String callService() {
String url = "https://example.com/service";
String response = httpsRestTemplate.getForObject(url, String.class);
return response;
}
上述代碼中,我們使用httpsRestTemplate進行調用,並將SSL相關的設置內部處理。
三、RestTemplate異常處理
RestTemplate在進行服務調用時可能會遇到各種異常情況,例如連接超時、請求超時、404等,這些異常都需要進行有效的處理。
1.連接超時
連接超時是指在指定時間內無法與目標服務建立連接的情況。
我們可以通過設置HttpComponentsClientHttpRequestFactory的超時選項來定義超時時間。
@Bean
public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory(HttpClient httpClient) {
HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
httpComponentsClientHttpRequestFactory.setConnectTimeout(5000);
httpComponentsClientHttpRequestFactory.setReadTimeout(5000);
return httpComponentsClientHttpRequestFactory;
}
上述代碼中,我們設置了連接超時時間為5秒,讀取超時時間同樣為5秒。
2.請求超時
請求超時是指向目標服務發出請求後,在指定時間內沒有收到響應的情況。
與連接超時類似,我們可以通過設置HttpComponentsClientHttpRequestFactory的超時選項來定義超時時間。
@Bean
public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory(HttpClient httpClient) {
HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
httpComponentsClientHttpRequestFactory.setConnectTimeout(5000);
httpComponentsClientHttpRequestFactory.setReadTimeout(5000);
return httpComponentsClientHttpRequestFactory;
}
上述代碼中,我們設置了請求超時時間為5秒。
3.404錯誤
在進行服務調用時,有時會遇到404錯誤,表示請求的資源不存在。
我們可以通過判斷HttpStatus,來進行404錯誤的處理。
@Autowired
private RestTemplate restTemplate;
public String callService() {
String url = "http://example.com/service";
ResponseEntity responseEntity = restTemplate.getForEntity(url, String.class);
if (responseEntity.getStatusCode() == HttpStatus.NOT_FOUND) {
return "404 Not Found";
}
return responseEntity.getBody();
}
上述代碼中,我們使用getForEntity方法進行調用,並通過HttpStatus.NOT_FOUND判斷是否遇到404錯誤。
四、RestTemplate的攔截器
在使用RestTemplate進行服務調用時,我們可能需要對請求進行一些統一的處理,例如添加公共的header、記錄請求日誌、重試機制等。
這時候,利用RestTemplate的攔截器機制可以非常方便地實現這些功能。
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestInterceptor loggingInterceptor) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.singletonList(loggingInterceptor));
return restTemplate;
}
@Bean
public ClientHttpRequestInterceptor loggingInterceptor() {
return new LoggingInterceptor();
}
private static class LoggingInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
// 記錄請求日誌
LOGGER.info("RestTemplate請求: {} {}", request.getMethod(), request.getURI());
if (body.length > 0) {
LOGGER.info("RestTemplate請求Body: {}", new String(body));
}
ClientHttpResponse response = execution.execute(request, body);
// 記錄響應日誌
LOGGER.info("RestTemplate響應: {}", response.getStatusCode());
if (response.getBody() != null) {
LOGGER.info("RestTemplate響應Body: {}", IOUtils.toString(response.getBody(), StandardCharsets.UTF_8));
}
return response;
}
}
}
上述代碼中,我們定義了一個LoggingInterceptor攔截器,實現了一個簡單的日誌記錄功能,在RestTemplate的配置中,我們將它作為一個攔截器進行註冊。
這樣,在每次RestTemplate進行服務調用時,都會經過LoggingInterceptor攔截器,以實現我們配置的統一處理。
五、總結
本文詳細介紹了RestTemplate的配置、使用、異常處理和攔截器等方面,通過本文的學習,我們可以更加熟練地使用RestTemplate進行服務調用,並能夠處理各種異常情況。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/272354.html
微信掃一掃
支付寶掃一掃