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());
}
相关推荐
m51272 分钟前
LinuxC语言
java·服务器·前端
IU宝6 分钟前
C/C++内存管理
java·c语言·c++
瓜牛_gn7 分钟前
依赖注入注解
java·后端·spring
hakesashou8 分钟前
Python中常用的函数介绍
java·网络·python
佚先森17 分钟前
2024ARM网络验证 支持一键云注入引流弹窗注册机 一键脱壳APP加固搭建程序源码及教程
java·html
古月居GYH31 分钟前
在C++上实现反射用法
java·开发语言·c++
儿时可乖了1 小时前
使用 Java 操作 SQLite 数据库
java·数据库·sqlite
ruleslol1 小时前
java基础概念37:正则表达式2-爬虫
java