当前位置:网站首页>Resttemplate use
Resttemplate use
2022-07-01 06:33:00 【A small salted fish I】
List of articles
1、 Introduce dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、 Add the configuration
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/** * RestTemplate Tool class , Mainly used to provide RestTemplate object */
@Configuration// Add this annotation to , Can be Spring scanning
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(500000);// Unit is ms
factory.setConnectTimeout(500000);// Unit is ms
return factory;
}
}
3、 Wrapper utility class
import com.alibaba.fastjson2.JSONObject;
import com.gc.api.entity.Rest;
import com.gc.common.utils.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.Map;
import java.util.Objects;
/** * @author sgc * @description RestTemplate Tool class * @date 2022/6/16 15:17 */
@Component
public class RestTemplateUtil {
@Autowired
protected LoginUtil loginUtil;
@Autowired
protected RestTemplate restTemplate;
/** * exchange send out post request The parameter for @requestBody Entity class application/json * * @param url Request path * @param object @requestBody Entity class * @return * @throws Exception */
public Rest exchangePostEntityReturnRest(String url, Object object) throws Exception {
// Set request header
HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("token", loginUtil.getToken());
resultRequestHeader.setContentType(MediaType.APPLICATION_JSON);
Map<String, Object> bean2Map = MapUtil.transBean2Map(object);
HttpEntity resultHttpEntity = new HttpEntity(bean2Map, resultRequestHeader);
ResponseEntity<Rest> result = restTemplate.exchange(url, HttpMethod.POST, resultHttpEntity, Rest.class);
if (Objects.nonNull(result.getBody())) {
return result.getBody();
}
return Rest.error(result);
}
/** * exchange send out post request The parameter for @requestBody Entity class application/json * * @param url Request path * @param object @requestBody Entity class * @return ResultMap * @throws Exception */
public ResultMap exchangePostEntityReturnResultMap(String url, Object object) throws Exception {
// Set request header
HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("token", loginUtil.getToken());
resultRequestHeader.setContentType(MediaType.APPLICATION_JSON);
Map<String, Object> bean2Map = MapUtil.transBean2Map(object);
HttpEntity resultHttpEntity = new HttpEntity(bean2Map, resultRequestHeader);
ResponseEntity<ResultMap> result = restTemplate.exchange(url, HttpMethod.POST, resultHttpEntity, ResultMap.class);
if (Objects.nonNull(result.getBody())) {
return result.getBody();
}
return null;
}
/** * exchange send out post request The parameter for @requestParam Of Map application/json * * @param map requestParam Parameter combined map for example : * Map<String, Object> map = new HashMap<>(); * map.put("unitGuid", unitGuid); * @return ResultMap * @throws Exception */
public ResultMap exchangePostRequestParamMapReturnResultMap(String url, Map<String, Object> map) throws Exception {
// Set request header
HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("token", loginUtil.getToken());
resultRequestHeader.setContentType(MediaType.APPLICATION_JSON);
HttpEntity resultHttpEntity = new HttpEntity(map, resultRequestHeader);
ResponseEntity<ResultMap> result = restTemplate.exchange(url, HttpMethod.POST, resultHttpEntity, ResultMap.class);
if (Objects.nonNull(result.getBody())) {
return result.getBody();
}
return null;
}
/** * exchange send out post request The parameter for @requestParam Parameters application/json * * @param url Request path * @param map requestParam Parameter combined map for example : * Map<String, Object> map = new HashMap<>(); * map.put("unitGuid", unitGuid); * @return Rest * @throws Exception */
public Rest exchangePostRequestParamReturnRest(String url, Map<String, Object> map) throws Exception {
// Set request header
HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("token", loginUtil.getToken());
// resultRequestHeader.setContentType(MediaType.APPLICATION_JSON);
String realUrl = url;
if (!map.isEmpty()) {
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
for (Map.Entry<String, Object> e : map.entrySet()) {
builder.queryParam(e.getKey(), e.getValue());
}
realUrl = builder.build().toString();
}
HttpEntity resultHttpEntity = new HttpEntity(null, resultRequestHeader);
ResponseEntity<Rest> result = restTemplate.exchange(realUrl, HttpMethod.POST, resultHttpEntity, Rest.class);
if (Objects.nonNull(result.getBody())) {
return result.getBody();
}
return Rest.error(result);
}
/** * exchange send out post request The parameter for @requestParam Parameters application/json * * @param url Request path * @param map requestParam Parameter combined map for example : * Map<String, Object> map = new HashMap<>(); * map.put("unitGuid", unitGuid); * @return ResultMap * @throws Exception */
public ResultMap exchangePostParamReturnResultMap(String url, Map<String, Object> map) throws Exception {
// Set request header
HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("token", loginUtil.getToken());
// resultRequestHeader.setContentType(MediaType.APPLICATION_JSON);
String realUrl = url;
if (!map.isEmpty()) {
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
for (Map.Entry<String, Object> e : map.entrySet()) {
builder.queryParam(e.getKey(), e.getValue());
}
realUrl = builder.build().toString();
}
HttpEntity resultHttpEntity = new HttpEntity(null, resultRequestHeader);
System.out.println(realUrl);
ResponseEntity<ResultMap> result = restTemplate.exchange(realUrl, HttpMethod.POST, resultHttpEntity, ResultMap.class);
if (Objects.nonNull(result.getBody())) {
return result.getBody();
}
return null;
}
/** * exchange send out get request The input parameter is an entity class application/json * * @param url Request path * @param object Entity class * @return Rest * @throws Exception */
public Rest exchangeGetEntityReturnRest(String url, Object object) throws Exception {
// Set request header
HttpHeaders headers = new HttpHeaders();
headers.add("token", loginUtil.getToken());
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
Map<String, Object> map = MapUtil.transBean2Map(object);
if (!map.isEmpty()) {
for (Map.Entry<String, Object> e : map.entrySet()) {
builder.queryParam(e.getKey(), e.getValue());
}
String realUrl = builder.build().toString(); // Splice the real request address here "?pageNo=1&pageSize=5"
HttpEntity<JSONObject> httpEntity = new HttpEntity<>(null, headers);
ResponseEntity<Rest> result = restTemplate.exchange(realUrl, HttpMethod.GET, httpEntity, Rest.class, map);
if (Objects.nonNull(result.getBody())) {
return result.getBody();
}
return Rest.error(result);
}
return Rest.error();
}
/** * exchange send out get request The input parameter is not marked @requestParam Parameters of ( Don't pass it on ) application/json * * @param url Request path * @param map param Parameter combined map for example : * Map<String, Object> map = new HashMap<>(); * map.put("unitGuid", unitGuid); * @return Rest * @throws Exception */
public Rest exchangeGetParamReturnRest(String url,Map<String,Object> map) throws Exception {
return exchangeGetRequestParamReturnRest(url,map);
}
/** * exchange send out get request Input parameters are marked @requestParam Parameters of application/json * * @param url Request path * @param map param Parameter combined map for example : * Map<String, Object> map = new HashMap<>(); * map.put("unitGuid", unitGuid); * @return Rest * @throws Exception */
public Rest exchangeGetRequestParamReturnRest(String url,Map<String,Object> map) throws Exception {
// Set request header
HttpHeaders headers = new HttpHeaders();
headers.add("token", loginUtil.getToken());
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
for (Map.Entry<String, Object> e : map.entrySet()) {
builder.queryParam(e.getKey(), e.getValue());
}
String realUrl = builder.build().toString(); // Splice the real request address here "?pageNo=1&pageSize=5"
System.out.println("realUrl==>"+realUrl);
HttpEntity<JSONObject> httpEntity = new HttpEntity<>(null, headers);
ResponseEntity<Rest> result = restTemplate.exchange(realUrl, HttpMethod.GET, httpEntity, Rest.class, map);
if (Objects.nonNull(result.getBody())) {
return result.getBody();
}
return Rest.error(result);
}
/** * exchange send out post request The input parameter is not marked @requestParam Parameters of application/json * * @param url Request path * @param map param Parameter combined map for example : * Map<String, Object> map = new HashMap<>(); * map.put("unitGuid", unitGuid); * @return Rest * @throws Exception */
public Rest exchangePostParamReturnRest(String url, Map<String, Object> map) throws Exception {
// Set request header
HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("token", loginUtil.getToken());
resultRequestHeader.setContentType(MediaType.APPLICATION_JSON);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
for (Map.Entry<String, Object> e : map.entrySet()) {
builder.queryParam(e.getKey(), e.getValue());
}
String realUrl = builder.build().toString();
HttpEntity resultHttpEntity = new HttpEntity(null, resultRequestHeader);
ResponseEntity<Rest> result = restTemplate.exchange(realUrl, HttpMethod.POST, resultHttpEntity, Rest.class);
if (Objects.nonNull(result.getBody())) {
return result.getBody();
}
return Rest.error(result);
}
/** * exchange send out post request Input parameters are marked @requestParam Of map Parameters application/json * * @param url Request path * @param map param Parameter combined map for example : * Map<String, Object> map = new HashMap<>(); * map.put("unitGuid", unitGuid); * @return ResultMap * @throws Exception */
public ResultMap exchangePostRequestMapReturnResultMap(String url, Map<String, Object> map) throws Exception {
// Set request header
HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("token", loginUtil.getToken());
resultRequestHeader.setContentType(MediaType.APPLICATION_JSON);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
for (Map.Entry<String, Object> e : map.entrySet()) {
builder.queryParam(e.getKey(), e.getValue());
}
String realUrl = builder.build().toString();
HttpEntity resultHttpEntity = new HttpEntity(null, resultRequestHeader);
ResponseEntity<ResultMap> result = restTemplate.exchange(realUrl, HttpMethod.POST, resultHttpEntity, ResultMap.class);
if (Objects.nonNull(result.getBody())) {
return result.getBody();
}
return null;
}
}
边栏推荐
- What is a port scanning tool? What is the use of port scanning tools
- [automatic operation and maintenance] what is the use of the automatic operation and maintenance platform
- ManageEngine Zhuohao helps you comply with ISO 20000 standard (IV)
- 谷粒商城-环境(p1-p27)
- 【微信小程序低代码开发】二,在实操中化解小程序的代码组成
- [self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
- JSON module
- json模块
- Self confidence is indispensable for technology
- Requests module (requests)
猜你喜欢
![[unity shader amplify shader editor (ASE) Chapter 9]](/img/f5/f0f6786406e149187e71c8e12cde0d.png)
[unity shader amplify shader editor (ASE) Chapter 9]

Lxml module (data extraction)

Forkjoin and stream flow test

Promise

概率论学习笔记

ManageEngine Zhuohao helps you comply with ISO 20000 standard (IV)

【微信小程序】如何搭积木式开发?

SystemVerilog learning-10-validation quantification and coverage

SQL statement

数据库产生死锁了请问一下有没有解决办法
随机推荐
C language course is provided with employee information management system (large operation)
码力十足学量化|如何在财务报告寻找合适的财务公告
[ManageEngine] how to realize network automatic operation and maintenance
手把手教你实现一个深度学习框架...
[ManageEngine Zhuohao] mobile terminal management solution, helping the digital transformation of Zhongzhou aviation industry
Student attendance system for C language course (big homework)
嵌入式系统
虚幻 简单的屏幕雨滴后处理效果
mysql学习
第五章 輸入/輸出(I/O)管理
C language course set up library information management system (big homework)
C language course set up salary management system (big homework)
NOC 设计的一些坑
10-golang运算符
lxml模块(数据提取)
[unity shader custom material panel part II]
【#Unity Shader#Amplify Shader Editor(ASE)_第九篇】
Picture server project test
SQL学习笔记九种连接2
数据库产生死锁了请问一下有没有解决办法