欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

restTemplate

程序员文章站 2022-03-02 17:32:55
...

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";
        }
    }