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

持续更新补充!!!

相关推荐
秋意钟3 分钟前
Spring新版本
java·后端·spring
椰椰椰耶5 分钟前
【文档搜索引擎】缓冲区优化和索引模块小结
java·spring·搜索引擎
mubeibeinv7 分钟前
项目搭建+图片(添加+图片)
java·服务器·前端
青莳吖8 分钟前
Java通过Map实现与SQL中的group by相同的逻辑
java·开发语言·sql
Buleall15 分钟前
期末考学C
java·开发语言
重生之绝世牛码17 分钟前
Java设计模式 —— 【结构型模式】外观模式详解
java·大数据·开发语言·设计模式·设计原则·外观模式
小蜗牛慢慢爬行23 分钟前
有关异步场景的 10 大 Spring Boot 面试问题
java·开发语言·网络·spring boot·后端·spring·面试
新手小袁_J1 小时前
JDK11下载安装和配置超详细过程
java·spring cloud·jdk·maven·mybatis·jdk11
呆呆小雅1 小时前
C#关键字volatile
java·redis·c#
Monly211 小时前
Java(若依):修改Tomcat的版本
java·开发语言·tomcat