restTemplate
1、get请求
第二个是返回值
SeoManager seo =restTemplate.getForObject(url + “getSeoManager?elementId={elementId}&appId={appId}”,SeoManager.class, id,getAtlasAppId());
2、普通post请求
第三个是返回值:
Long atlasId = restTemplate.postForObject(this.atlasRepository + “add”, atlas, Long.class);
3、请求参数为空
String str = template.postForObject(url+param,null, String.class);
4、没有返回值
1)、restTemplate.put(url+“deleteComplaintPageBigField?arg0={arg0}”,null, complaintPageContent.getId());
2)、HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 在header里面设置编码方式
HttpEntity requestEntity = new HttpEntity(oldContent , headers);
restTemplate.put(this.complaintPageContentRepository + “updateComplaintPageContent”, requestEntity);
5、返回值为list集合
服务端用@RequestBody 接收
ParameterizedTypeReference<List<ComplanitXtree>> typeRef = new ParameterizedTypeReference<List<ComplanitXtree>>() {
};
ResponseEntity<List<ComplanitXtree>> responseEntity = restTemplate.exchange(this.complaintPageCategoryRepository + "getCateForXTree", HttpMethod.POST, null, typeRef);
List<ComplanitXtree> topNodes = responseEntity.getBody();
6、可变长度参数
public Object delete(@RequestParam(value = "ids")Long... ids) {
try {
String rs = restTemplate.getForObject(this.complaintPageContentRepository + "deleteComplaintPageContent?ids={ids}", String.class,StringUtils.join(ids, ","));
if("success".equals(rs)){
return responebodyResult(HttpStatus.OK);
}else{
return responsebodyError();
}
} catch (Exception e) {
e.printStackTrace();
logger.error("delete",e);
return responsebodyError();
}
}
@RequestMapping("/deleteComplaintPageContent")
public String deleteComplaintPageContent(Long... ids) {
try {
complaintPageContentDao.deleteComplaintPageRelated(ids);
complaintPageContentDao.deletes(ids);
complaintPageBigFieldDao.deletes(ids);
return "success";
} catch (Exception e) {
e.printStackTrace();
return "false";
}
}
推荐阅读
-
Spring Boot使用RestTemplate消费REST服务的几个问题记录
-
Spring cloud restTemplate 传递复杂参数的方式(多个对象)
-
Spring Boot使用RestTemplate消费REST服务的几个问题记录
-
详解SpringBoot通过restTemplate实现消费服务
-
Spring cloud restTemplate 传递复杂参数的方式(多个对象)
-
Spring RestTemplate具体使用详解
-
详解SpringBoot通过restTemplate实现消费服务
-
基于RestTemplate的使用方法(详解)
-
基于RestTemplate的使用方法(详解)
-
Eureka restTemplate访问超时