调用方式
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());
持续更新补充!!!