SpringBoot RestTemplate远程调用总结

1、get请求

复制代码
@GetMapping("/searchEntryRecordPageList")
public JSONObject searchEntryRecordPageList(@RequestParam Map<String,Object> params){
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.add("Authorization","Bearer "
    + String.valueOf(params.get("access_token")));
    MultiValueMap<String, Object> entryRecordParam = 
    new LinkedMultiValueMap<String, Object>();
    entryRecordParam.add("offset",params.get("offset"));
    entryRecordParam.add("limit",params.get("limit"));
    entryRecordParam.add("yzOpenId",params.get("yzOpenId"));
    HttpEntity<MultiValueMap<String, Object>> requestEntity = 
    new HttpEntity<MultiValueMap<String, Object>>(entryRecordParam, requestHeaders);
    ResponseEntity<Map> response = null;
    try {
        response =         restTemplate.exchange(yzymUrl+"/app/entryRecord/searchEntryRecordPageList",         HttpMethod.POST, requestEntity, Map.class);
    }catch (Exception e){
        return exceptionObj(e.getMessage());
    }
    logger.info(JSONObject.toJSONString(response.getBody()));
    return (JSONObject) JSONObject.toJSON(response.getBody());
}

@GetMapping("/searchCheckResultByEntryRecordId")
public JSONObject searchCheckResultByEntryRecordId(@RequestParam 
Map<String,Object> params){
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.add("Authorization","Bearer "
    + String.valueOf(params.get("access_token")));
    MultiValueMap<String, Object> entryRecordParam = 
    new LinkedMultiValueMap<String, Object>();
    HttpEntity<MultiValueMap<String, Object>> requestEntity = 
    new HttpEntity<MultiValueMap<String, Object>>(entryRecordParam, requestHeaders);
    ResponseEntity<Map> response = null;
    try {
        response =     restTemplate.exchange(yzymUrl+"/app/entryRecord/searchCheckResultByEntryRecordId?entryRecordId="+params.get("entryRecordId"), 
HttpMethod.GET, requestEntity, Map.class);
    }catch (Exception e){
        return exceptionObj(e.getMessage());
    }
    logger.info(JSONObject.toJSONString(response.getBody()));
    return (JSONObject) JSONObject.toJSON(response.getBody());
}

2、post请求

复制代码
@PostMapping("/batchAddCart")
public JSONObject addCart(@RequestParam Map<String,Object> params){
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.add("Authorization","Bearer "
    + String.valueOf(params.get("access_token")));
    MultiValueMap<String, Object> entryRecordParam = 
    new LinkedMultiValueMap<String, Object>();
    entryRecordParam.add("yzOpenId",params.get("yzOpenId"));
    entryRecordParam.add("cartParams",params.get("cartParams"));
    HttpEntity<MultiValueMap<String, Object>> requestEntity = 
    new HttpEntity<MultiValueMap<String, Object>>(entryRecordParam, requestHeaders);
    ResponseEntity<Map> response = null;
    try {
        response = restTemplate.exchange(yzymUrl+"/app/yzGoods/batchAddCart",         HttpMethod.POST, requestEntity, Map.class);
    }catch (Exception e){
        return exceptionObj(e.getMessage());
    }
    logger.info(JSONObject.toJSONString(response.getBody()));
    return (JSONObject) JSONObject.toJSON(response.getBody());
}
相关推荐
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭6 分钟前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫23 分钟前
泛型(2)
java
南宫生32 分钟前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石40 分钟前
12/21java基础
java
李小白661 小时前
Spring MVC(上)
java·spring·mvc
GoodStudyAndDayDayUp1 小时前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea
装不满的克莱因瓶2 小时前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
n北斗2 小时前
常用类晨考day15
java
骇客野人2 小时前
【JAVA】JAVA接口公共返回体ResponseData封装
java·开发语言
AskHarries2 小时前
Spring Cloud OpenFeign快速入门demo
spring boot·后端