首页 > 其他 > 详细

RestTemplate get请求多参数 简单封装

时间:2019-12-03 16:44:13      阅读:594      评论:0      收藏:0      [点我收藏+]

使用RestTemplate发送get请求时,如果有多个参数拼接起来会比较麻烦,在此做个简单的封装

public static void main(String[] args) {
    Map<String, Object> paramMap = new HashMap<>(16);
    paramMap.put("userId", "8a0bb0a698c142420198c15a7e5b0001");
    paramMap.put("pageNum", "1");
    paramMap.put("pageSize", "3");
    paramMap.put("createStartTime", "2029-08-09");
    paramMap.put("createEndTime", null);
    //获取积分明细
    ObjectDataResponse objectDataResponse = getForObject("http://10.253.40.157:8180/manage/scoreDetail", paramMap);
    System.out.println(objectDataResponse);
}

/**
 * 封装的get请求,暂时只支持map传参
 *
 * @param url
 * @param object
 * @return
 */
private static ObjectDataResponse getForObject(String url, Object object) {
    StringBuffer stringBuffer = new StringBuffer(url);
    if (object instanceof Map) {
        Iterator iterator = ((Map) object).entrySet().iterator();
        if (iterator.hasNext()) {
            stringBuffer.append("?");
            Object element;
            while (iterator.hasNext()) {
                element = iterator.next();
                Map.Entry<String, Object> entry = (Map.Entry) element;
                //过滤value为null,value为null时进行拼接字符串会变成 "null"字符串
                if (entry.getValue() != null) {
                    stringBuffer.append(element).append("&");
                }
                url = stringBuffer.substring(0, stringBuffer.length() - 1);
            }
        }
    } else {
        throw new RuntimeException("url请求:" + url + "请求参数有误不是map类型");
    }
    log.info("url请求:" + url);
    return new RestTemplate().getForObject(url, ObjectDataResponse.class);
}

请求结果

 

16:31:08.803 [main] INFO com.smf.interfaces.ManagementController - url请求:http://10.253.40.157:8180/manage/scoreDetail?createStartTime=2029-08-09&pageSize=10&userId=8a0bb0a698c142420198c15a7e5b0001&pageNum=1
16:31:09.208 [main] DEBUG org.springframework.web.client.RestTemplate - Created GET request for "http://10.253.40.157:8180/manage/scoreDetail?createStartTime=2029-08-09&pageSize=10&userId=8a0bb0a698c142420198c15a7e5b0001&pageNum=1"
16:31:09.271 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json]
16:31:09.834 [main] DEBUG org.springframework.web.client.RestTemplate - GET request for "http://10.253.40.157:8180/manage/scoreDetail?createStartTime=2029-08-09&pageSize=10&userId=8a0bb0a698c142420198c15a7e5b0001&pageNum=1" resulted in 200 (null)
16:31:09.835 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [class com.smf.model.response.ObjectDataResponse] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@1dde4cb2]
ObjectDataResponse(super=RestfulResponse(super=com.smf.model.response.ObjectDataResponse@76b92ca8, code=20000, msg=成功), data={pageNum=1, pageSize=10, total=3, totalPage=1, rows=[{createTime=2037.12.06 02:55:38, num=1, name=活动发放积分, updateTime=null, scoreValue=+500, ROW_ID=1}, {createTime=2037.12.06 02:55:38, num=2, name=活动发放积分, updateTime=null, scoreValue=+500, ROW_ID=2}, {createTime=2037.12.06 02:55:38, num=3, name=活动发放积分, updateTime=null, scoreValue=+500, ROW_ID=3}]})

 

RestTemplate get请求多参数 简单封装

原文:https://www.cnblogs.com/yuancoco/p/11977310.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!