前言

在使用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
);
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐