【RestTemplate】关于getForObject()的正确用法

前言

 

在使用RestTemplate的getForObject()方法时一直报错,原来是因为使用map传参需要固定RestTemplate访问的url格式。比如我想携带appId和appKey这两个参数,就得在url里面显示声明出来,特此记录一下

 

解决

RestTemplate restTemplate = null;
InfoResponse response = null;

restTemplate = GenericObjectPoolUtils.borrowObject(RestTemplate.class);

Map<String, String> hashMap = new HashMap<>(5);
hashMap.put("appId", appId);
hashMap.put("appKey", appKey);

response = restTemplate.getForObject(
        "http://localhost:8083/api/getinfo?appId={appId}&appKey={appKey}"
        InfoResponse.class, hashMap
);

 

posted @ 2022-06-14 09:35  南北12345678  阅读(2076)  评论(0编辑  收藏  举报