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

持续更新补充!!!

相关推荐
running up that hill3 分钟前
数据库中库的操作
java·数据库·mysql
paterWang23 分钟前
小程序-基于java+SpringBoot+Vue的实习生管理系统设计与实现
java·spring boot·小程序
we_前端全家桶26 分钟前
小程序中模拟发信息输入框,让textarea可以设置最大宽以及根据输入的内容自动变高的方式
java·前端·小程序
paterWang29 分钟前
小程序-基于java+SpringBoot+Vue的经济新闻资讯设计与实现
java·spring boot·小程序
ModelBulider35 分钟前
十四、SpringMVC的执行流程
java·开发语言·后端·spring·springmvc
喜欢猪猪1 小时前
Spring Boot开箱即用可插拔实现过程演练与原理剖析
java·spring boot·后端
MisterZhang6661 小时前
java中rag使用mysql作为向量数据库,存储读写如何优化
java·数据库·mysql
CC大煊1 小时前
【设计模式】模板方法模式 在java中的应用
java·设计模式·模板方法模式
DBdoctor官方1 小时前
Redis/Codis性能瓶颈揭秘:网卡软中断的影响与优化
java·数据库·redis·sql·mysql·php·dbdcotor
AskHarries2 小时前
Spring Boot集成ShedLock实现分布式定时任务
java·spring boot·后端