1.设置get方法
ResponseEntity<JSONObject> responseEntity= restTemplate.getForEntity(url,JSONObject.class);
2.设置post方法
MultiValueMap map = new LinkedMultiValueMap();
map.add("name", name);
ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url, map, JSONObject.class);
3.put方法
MultiValueMap map = new LinkedMultiValueMap();
map.add("username", "username");
map.add("address", "address");
restTemplate.put(url, map);
4.delete方法
restTemplate.delete(url, model);
5.配置文件(无配置不可使用)
public class RestConfig {
//不重要
@Bean
public CloseableHttpClient httpClient() throws Exception {
SSLContext context = new SSLContextBuilder().loadTrustMaterial(null, (x509Certificates, s) -> true).build();
SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(context, NoopHostnameVerifier.INSTANCE);
return HttpClients.custom()
.setSSLSocketFactory(factory)
.build();
}
//不重要
@Bean
public ClientHttpRequestFactory httpRequestFactory() throws Exception {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient());
factory.setConnectionRequestTimeout(3000);
factory.setReadTimeout(30000);
factory.setConnectTimeout(30000);
return factory;
}
//必须要
@Bean
public RestTemplate restTemplate() throws Exception {
return new RestTemplate(httpRequestFactory());
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/260347.html