调用第三方接口-RestTemplate

调用方式

POST请求

单个新增

例如后端接口接收参数为 User user

使用RestTemplate发送post请求,暂未进行异常处理

java 复制代码
//封装body信息
JsonObject jsonObject = new JsonObject();
jsonObject.put("userName","张三");
jsonObject.put("city","北京");


//选择性设置请求头header信息,token
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization","bearer redaijDffADJewee23Sd");
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<JsonObject> httpEntity = new HttpEntity<>(jsonObject,httpHeaders);

String url = "请求接口url"
//发送post请求
ResponseEntity<JsonObject> responseEntity = restTemplate.postForEntity(url, httpEntity, JsonObject.class);
if(responseEntity.getStatusCode().is2xxSucessful() && responseEntity.getBody() != null){
	JsonObject bodyData = responseEntity.getBody();
	System.out.println("请求数据:"+ bodyData + ",请求接口url:" + url);
}

批量新增

例如后端接口接收参数为List users

使用RestTemplate发送post请求

java 复制代码
JsonArray jsonArray = new JsonArray();
//封装body信息
JsonObject jsonObject1 = new JsonObject();
jsonObject1.put("userName","张三");
jsonObject1.put("city","北京");
jsonArray.add(jsonObject1);

JsonObject jsonObject2 = new JsonObject();
jsonObject2.put("userName","朱四");
jsonObject2.put("city","南京");
jsonArray.add(jsonObject2);

//String body = jsonArray.toJsonString();

//选择性设置请求头header信息,token
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization","bearer redaijDffADJewee23Sd");
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
//HttpEntity<String> httpEntity = new HttpEntity<>(body,httpHeaders);
HttpEntity<JsonArray> httpEntity = new HttpEntity<>(jsonArray,httpHeaders);

String url = "请求接口url"
//发送post请求
ResponseEntity<JsonObject> responseEntity = restTemplate.postForEntity(url, httpEntity, JsonObject.class);
if(responseEntity.getStatusCode().is2xxSucessful() && responseEntity.getBody() != null){
	JsonObject bodyData = responseEntity.getBody();
	System.out.println("请求数据:"+ bodyData + ",请求接口url:" + url);
}

GET请求

1.含@PathVariable

服务端接口http://12.131.23.1/user/get/{userId}

参数(PathVariable("userId") String userId)

java 复制代码
String url = "http://12.131.23.1/user/get/{userId}";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization","bearer redaijDffADJewee23Sd");
String httpBody = null;
HttpEntity<String> httpEntity = new HttpEntity<>(httpBody,httpHeaders);
ResponseEntity<JsonObject> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, JsonObject.class,"userId");
System.out.println(responseEntity.getBody());

2.查询列表

使用RestTemplate发送GET请求,如查询列表

服务端接口http://12.131.23.1/user/list

参数(UserReqDto userReqDto)

java 复制代码
String url = "http://12.131.23.1/user/list?userName={userName}"

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization","bearer redaijDffADJewee23Sd");
String httpBody = null;
HttpEntity<String> httpEntity = new HttpEntity<>(httpBody,httpHeaders);

Map<String,Object> queryMap = new HashMap<>();
queryMap.put("userName","张三");
ResponseEntity<JsonObject> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, JsonObject.class, queryMap);
System.out.println(responseEntity.getBody());

持续更新补充!!!

相关推荐
蜗牛^^O^37 分钟前
Docker和K8S
java·docker·kubernetes
从心归零1 小时前
sshj使用代理连接服务器
java·服务器·sshj
IT毕设梦工厂2 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
Ylucius3 小时前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
七夜zippoe3 小时前
分布式系统实战经验
java·分布式
是梦终空3 小时前
JAVA毕业设计176—基于Java+Springboot+vue3的交通旅游订票管理系统(源代码+数据库)
java·spring boot·vue·毕业设计·课程设计·源代码·交通订票
落落落sss3 小时前
sharding-jdbc分库分表
android·java·开发语言·数据库·servlet·oracle
码爸3 小时前
flink doris批量sink
java·前端·flink
Monodye4 小时前
【Java】网络编程:TCP_IP协议详解(IP协议数据报文及如何解决IPv4不够的状况)
java·网络·数据结构·算法·系统架构
一丝晨光4 小时前
逻辑运算符
java·c++·python·kotlin·c#·c·逻辑运算符