调用第三方接口-RestTemplate

POST请求

单个新增

例如后端接口接收参数为 User user
使用RestTemplate发送post请求,暂未进行异常处理

//封装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请求

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)

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)

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());

持续更新补充!!!

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用RestTemplate调用第三方接口。下面是一个简单的示例代码: ```java import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; public class RestTemplateExample { public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); // 设置请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); // 可以设置其他请求头参数 // 构建请求实体 HttpEntity<String> requestEntity = new HttpEntity<>(headers); // 发送GET请求 ResponseEntity<String> responseEntity = restTemplate.exchange("http://api.example.com/endpoint", HttpMethod.GET, requestEntity, String.class); // 获取响应结果 String response = responseEntity.getBody(); System.out.println(response); } } ``` 在上面的示例中,我们首先创建了一个RestTemplate对象。然后,我们设置了请求头参数,例如Content-Type。接下来,我们构建了一个HttpEntity对象,将请求头信息和请求体(如果有)传递给RestTemplate的exchange方法。最后,我们可以通过responseEntity对象获取响应结果。 你需要根据自己的需求来设置请求方法(如GET、POST等)、URL、请求头参数、请求体内容等。另外,你还可以根据具体的接口返回类型来调整exchange方法的第四个参数类型。 希望这个简单示例对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值