網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1、配置類(lèi)
@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;
}
}
實(shí)際使用時(shí)可以生成不同的factory
// 不進(jìn)行證書(shū)校驗(yàn)
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、發(fā)送請(qǐng)求
(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沒(méi)法直接攜帶header
(3)restTemplate.postForEntity
HttpEntity<EspaceMessageDTO> httpEntity = new HttpEntity<>(espaceMessageDTO,headers);
ResponseEntity<Object> res = restTemplate.postForEntity(url,httpEntity, Object.class);
(4)接收的類(lèi)帶泛型
ResponseEntity<List<RundeckTaskDO>> result = restTemplate.exchange(restful, HttpMethod.GET, entity, new ParameterizedTypeReference<List<RundeckTaskDO>>() {
});
原文鏈接:https://blog.csdn.net/qq_37831759/article/details/140024485
- 上一篇:沒(méi)有了
- 下一篇:沒(méi)有了
相關(guān)推薦
- 2022-10-14 SpringCloud 微服務(wù)與遠(yuǎn)程調(diào)用測(cè)試
- 2023-02-26 詳解pandas中Series()和DataFrame()的區(qū)別與聯(lián)系_python
- 2022-10-13 Broadcast廣播機(jī)制在Pytorch?Tensor?Numpy中的使用詳解_python
- 2022-05-20 如何保證緩存和數(shù)據(jù)庫(kù)的一致性?
- 2022-11-03 Python中的def?__init__(?)函數(shù)_python
- 2022-03-22 C++運(yùn)算符重載詳情介紹_C 語(yǔ)言
- 2022-11-22 Linux命令學(xué)習(xí)之原來(lái)最簡(jiǎn)單的ls命令這么復(fù)雜_linux shell
- 2023-01-14 C/C++高精度(加減乘除)算法的實(shí)現(xiàn)_C 語(yǔ)言
- 欄目分類(lèi)
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支