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,null, String.class);
linker = restTemplate.getForObject(url,BossLinkMan.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();
ParameterizedTypeReference<List<ComplaintPageCategory>> typeRef = new ParameterizedTypeReference<List<ComplaintPageCategory>>() {
            };
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 在header里面设置编码方式
            HttpEntity requestEntity = new HttpEntity(Long.valueOf(parentId) , headers);

            ResponseEntity<List<ComplaintPageCategory>> responseEntity = restTemplate.exchange(this.complaintPageCategoryRepository + "getCateAllChildNode", HttpMethod.POST, requestEntity, typeRef);
            List<ComplaintPageCategory> allComplaintPageCategory = responseEntity.getBody();



/**
	 * 根据id获取当前节点的所有子节点
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getCateAllChildNode")
	public List<ComplaintPageCategory> getCateAllChildNode(@RequestBody Long id) {
		// 获取所有分类数据
		List<ComplaintPageCategory> introcate = complaintPageCategoryDao.list(null);
		// 找到当前节点所有子节点
		List<ComplaintPageCategory> returnList = new ArrayList<ComplaintPageCategory>();
		return this.getCategorysAllChildNode(introcate, returnList, id);
	}

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

7、传参为对象

{
	"atlasCategory":{
		"showListStyle":"",
		"appid":"4",
		"authData":"[{\"authType\"\":\"\"1\",\"authStr\":\"GW_:4:category:view:\",\"roleIds\":\"\"}]"
	},
	"seoState":"1",
	"seoAddDescription":"",
	"strSeoTitle":"[{\"id\":\"categoryName\",\"name\":\"图册分类名称\"},{\"id\":\"oneCategoryName\",\"name\":\"图册分类一级分类名称\"},{\"id\":\"siteName\",\"name\":\"网站名称\"}]",
	"strSeoKeywords":"[{\"id\":\"categoryKeyword\",\"name\":\"图册分类关键词\"},{\"id\":\"siteName\",\"name\":\"网站名称\"}]",
	"strSeoDescription":"[{\"id\":\"categoryName\",\"name\":\"图册分类名称\"},{\"id\":\"siteName\",\"name\":\"网站名称\"},{\"id\":\"categoryDescription\",\"name\":\"图册分类描述\"}]",
	"strSeoTitleSign":"_",
	"strSeoKeywordsSign":",",
	"strSeoDescriptionSign":"-",
	"seoPageType":"2"
}
public Object add(@RequestBody Map<String, Object> requestParams,HttpServletRequest request) {
        JSONObject atlasCategoryJson= (JSONObject)requestParams.get("atlasCategory");
        AtlasCategory atlasCategory = JSON.toJavaObject(atlasCategoryJson,AtlasCategory.class);

8、传参为list

{
	"atlasImgs": [{
		"name": "9d5f211017c040bc901ec0811ae7928c",
		"height": "2545",
		"width": "2670",
		"id": "15574584579339419",
		"des": "",
		"relativeImgUrl": "repository/image/8uHjuUWFRJ61Vlh9XR3K-w.png",
		"relativeThumbUrl": "repository/image/8uHjuUWFRJ61Vlh9XR3K-w.png",
		"thumImgloadErrorFlag": false,
		"imgloadErrorFlag": false,
		"targetFlag": "true",
		"linkUrl": "",
		"title": "",
		"imgUrl": "/repository/image/8uHjuUWFRJ61Vlh9XR3K-w.png",
		"thumbUrl": "/repository/image/8uHjuUWFRJ61Vlh9XR3K-w.png_135x135.jpg",
		"coverFlag": "true",
		"showOrder": "0",
		"imgId": "58",
		"thumbId": "58"
	}],
	"commonAtlasName": "维持2",
	"pic_title": "",
	"pic_linkUrl": "",
	"pic_des": "",
	"publishDate": "",
	"publishMobileDate": "",
	"commonAtlasDescription": "<p>\n\t2</p>\n",
	"commonMobileAtlasDescription": "<p>\n\t2</p>\n",
	"summary": "",
	"keywords": "",
	"authStr": "GW_:4:detail:view:",
	"authType": "1",
	"roleIds": "",
	"authData": "[{\"authType\":\"1\",\"authStr\":\"GW_:4:detail:view:\",\"roleIds\":\"\"}]",
	"seoState": "1",
	"seoAddDescription": "",
	"viewCountOfCommonAtlas": "0",
	"isSpread": "no",
	"showFlagOfCommonAtlas": "1",
	"showMobileFlagOfCommonAtlas": "1",
	"description": "",
	"descriptionMobile": "",
	"commonAtlasSummary": "",
	"showStyle": "",
	"showMobileStyle": "",
	"seoPageType": "3",
	"atlasType": "0",
	"atlasCategoryArr": ["2"],
	"relevantContentList": [{
		"appId": 4,
		"contentList": []
	}],
	"videoIds": "",
	"seoTitle": "[{\"id\":\"name\",\"name\":\"图册名称\"},{\"id\":\"siteName\",\"name\":\"网站名称\"}]",
	"seoKeywords": "[{\"id\":\"keyword\",\"name\":\"图册关键词\"},{\"id\":\"name\",\"name\":\"图册名称\"},{\"id\":\"siteName\",\"name\":\"网站名称\"}]",
	"seoDescription": "[{\"id\":\"name\",\"name\":\"图册名称\"},{\"id\":\"siteName\",\"name\":\"网站名称\"},{\"id\":\"summary\",\"name\":\"图册概要\"}]",
	"seoTitleSign": "_",
	"seoKeywordsSign": ",",
	"seoDescriptionSign": "-"
}
JSONArray imgsJson= (JSONArray)param.get("atlasImgs");
List<Map<String, Object>> imgs = JSON.toJavaObject(imgsJson,ArrayList.class);

9、传参为数组

RemindValue[] remindValue = paramVo.getRemindValue();

Map<String, Object> requestEntity = new HashMap<>();
requestEntity.put("remindValueStr", remindValue);
requestEntity.put("remindValueFormlist", remindValueForms);
restTemplate.postForObject(this.siteRemindRepository+"saveSiteRemindAndForm", requestEntity, boolean.class);

JSONArray remindValueJson= (JSONArray)requestParams.get("remindValueStr");
RemindValue[] remindValue = JSON.toJavaObject(remindValueJson, RemindValue[].class);

10、传参为map

JSONObject modelJson= (JSONObject)requestParams.get("model");
Map<String, String> model = JSONObject.parseObject(modelJson.toJSONString(), new TypeReference<Map<String, String>>(){});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值