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());
}
相关推荐
v***553414 分钟前
springboot使用logback自定义日志
java·spring boot·logback
qq_3363139316 分钟前
java基础-集合进阶
java·开发语言·windows
稚辉君.MCA_P8_Java17 分钟前
Gemini永久会员 归并排序(Merge Sort) 基于分治思想(Divide and Conquer)的高效排序算法
java·linux·算法·spring·排序算法
q***188417 分钟前
Spring Boot中的404错误:原因、影响及处理策略
java·spring boot·后端
222you18 分钟前
MybatisPlus常用注解
java·开发语言·spring
汤姆Tom33 分钟前
前端转战后端:JavaScript 与 Java 对照学习指南 (第一篇 - 深度进阶版)
java·javascript
4***175437 分钟前
Spring Boot整合WebSocket
spring boot·后端·websocket
济宁雪人39 分钟前
Java安全基础——JNI安全基础
java·开发语言
q***96581 小时前
Java进阶-在Ubuntu上部署SpringBoot应用
java·spring boot·ubuntu
h***06651 小时前
【JSqlParser】Java使用JSqlParser解析SQL语句总结
java·开发语言·sql