springboot-restTemplate

来源:互联网 发布:阿提拉全面战争知乎 编辑:程序博客网 时间:2024/06/05 11:38

学习时使用的是spring,到公司后使用的框架是spring的升级版,springboot在工程中使用时将前后台分开,前台调用前台工程的Controller,之后使用restTemplate ,再去调用后台工程的控制层。

restTemplate 使用时发现无法传递list。当时不知道具体的原理。只是发现接收后类型变成hashMap。解决的方法是转成json。传递字符串。接收时也接收为字符串后使用JSON.parseObject转化为list集合。

后经人指点restTemplate 将请求转化url请求,list在其中就只能以hashMap的形式存在。另一个办法是发送请求时,定义请求为post请求。

或者使用

restTemplate.exchange(targetUrl, HttpMethod.GET, null,/*声明了返回类型*/new ParameterizedTypeReference<<List<>>>() {}).getBody()
可以直接接收list。

另json转list后,虽然外层类型转化为了List,但如果单独调其中的对象的话,会发现其实还是hashMap而不是对象。

2016-11-22

0 0
原创粉丝点击