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());
}
相关推荐
沈询-阿里36 分钟前
java-智能识别车牌号_基于spring ai和开源国产大模型_qwen vl
java·开发语言
AaVictory.42 分钟前
Android 开发 Java中 list实现 按照时间格式 yyyy-MM-dd HH:mm 顺序
android·java·list
Yaml41 小时前
智能化健身房管理:Spring Boot与Vue的创新解决方案
前端·spring boot·后端·mysql·vue·健身房管理
LuckyLay1 小时前
Spring学习笔记_27——@EnableLoadTimeWeaving
java·spring boot·spring
向阳12181 小时前
Dubbo负载均衡
java·运维·负载均衡·dubbo
Gu Gu Study2 小时前
【用Java学习数据结构系列】泛型上界与通配符上界
java·开发语言
WaaTong2 小时前
《重学Java设计模式》之 原型模式
java·设计模式·原型模式
m0_743048442 小时前
初识Java EE和Spring Boot
java·java-ee
AskHarries2 小时前
Java字节码增强库ByteBuddy
java·后端
佳佳_2 小时前
Spring Boot 应用启动时打印配置类信息
spring boot·后端