網站首頁 編程語言 正文
1、配置類
@Configuration
public class RestTemplateConfig() {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(300000);
factory.setReadTimeout(300000);
return factory;
}
}
實際使用時可以生成不同的factory
// 不進行證書校驗
RestTemplate restTemplateHttps = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());
public static HttpComponentsClientHttpRequestFactory generateHttpRequestFactory()
throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
new NoopHostnameVerifier());
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient);
return factory;
}
2、發送請求
(1)restTemplate.exchange
// get
HttpEntity<MyDTO> httpEntity = new HttpEntity<>(inputBody,headers)
ResponseEntity<Map> exchange = restTemplate.exchange(url,HttpMethod.GET,httpEntity,Map.class)
// Post
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<Map<String, Object>>(params, headers);
ResponseEntity<refreshUeResultDTO> ret = restTemplate.exchange(restfulUrl, HttpMethod.POST, httpEntity,
refreshUeResultDTO.class);
(2) restTemplate.getForEntity
ResponseEntity<String> ret = restTemplate.getForEntity(restful, String.class, new HashMap<>());
getForEntity沒法直接攜帶header
(3)restTemplate.postForEntity
HttpEntity<EspaceMessageDTO> httpEntity = new HttpEntity<>(espaceMessageDTO,headers);
ResponseEntity<Object> res = restTemplate.postForEntity(url,httpEntity, Object.class);
(4)接收的類帶泛型
ResponseEntity<List<RundeckTaskDO>> result = restTemplate.exchange(restful, HttpMethod.GET, entity, new ParameterizedTypeReference<List<RundeckTaskDO>>() {
});
原文鏈接:https://blog.csdn.net/qq_37831759/article/details/140024485
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-09-19 Golang如何編寫內存高效及CPU調優的Go結構體_Golang
- 2022-12-05 Python中的int函數使用_python
- 2022-10-15 Qt?TCP網絡通信學習_C 語言
- 2023-01-31 golang優先級隊列的實現全過程_Golang
- 2022-04-23 Docker?Compose快速部署多容器服務實戰的實例詳解_docker
- 2022-12-10 C++的std::vector<bool>轉儲文件問題_C 語言
- 2022-04-04 【mybatis】spring mybatis與pageHelper分頁插件的整合
- 2022-09-26 css外邊距問題和顯示模式
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支