restTemplate如何返回List数据呢?

戚薇 Java经验 发布时间:2023-05-16 16:20:13 阅读数:3906 1
下文笔者讲述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;
    }
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202305/16842252556471.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者