调用第三方接口-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());

持续更新补充!!!

相关推荐
customer084 小时前
【开源免费】基于SpringBoot+Vue.JS体育馆管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
Miketutu5 小时前
Spring MVC消息转换器
java·spring
乔冠宇5 小时前
Java手写简单Merkle树
java·区块链·merkle树
LUCIAZZZ6 小时前
简单的SQL语句的快速复习
java·数据库·sql
komo莫莫da7 小时前
寒假刷题Day19
java·开发语言
S-X-S7 小时前
算法总结-数组/字符串
java·数据结构·算法
linwq87 小时前
设计模式学习(二)
java·学习·设计模式
桦说编程8 小时前
CompletableFuture 超时功能有大坑!使用不当直接生产事故!
java·性能优化·函数式编程·并发编程
@_@哆啦A梦8 小时前
Redis 基础命令
java·数据库·redis
字节全栈_rJF9 小时前
性能测试 —— Tomcat监控与调优:status页监控_tomcat 自带监控
java·tomcat