restTemplate如何返回List数据呢?
下文笔者讲述restTemplate返回list数据的方法分享,如下所示
restTemplate返回list数据的实现思路
1.只需在调用restTemplate时, 传入responseType为返回的实体类 2.获取返回的body信息,即为list信息例:restTemplate返回list数据
//HTTP GET(一)方式 public List<UserInfoFeedback> findUserInfoFeedbackListByMerid(String userid) { HttpHeaders headers = new HttpHeaders(); headers.set("Accept", "application/json"); HttpEntity entity = new HttpEntity(headers); Map<String, String> params = new HashMap<String, String>(); params.put("userid", userid); String url = restServerAdd + "/test/findUserInfoFeedbackListByMerid?userid="+userid; ParameterizedTypeReference<List<UserInfoFeedback>> responseType = new ParameterizedTypeReference<List<UserInfoFeedback>>() {}; ResponseEntity<List<UserInfoFeedback>> resp = restTemplate.exchange(url, HttpMethod.GET, entity, responseType,params); List<UserInfoFeedback> list = resp.getBody(); return list; } //HTTP GET(二)方式 public List<UserInfo> getUserList() { String url = restServerAdd + "/user/getUserList"; UserInfo[] model = restTemplate.getForObject(url, UserInfo[].class); return Arrays.asList(model); } //HTTP POST方式 public List<UserInfo> getUserList(String uid) { String url = restServerAdd + "/user/getUserList"; MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>(); paramMap.add("uid", uid); HttpHeaders headers = new HttpHeaders(); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap, headers); ResponseEntity<List<UserInfo>> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, new ParameterizedTypeReference<List<UserInfo>>() { }); List<UserInfo> checks = response.getBody(); return checks; }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。