使用Spring RestTemplate获取JSON对象的列表 [英] Get list of JSON objects with Spring RestTemplate

查看:203
本文介绍了使用Spring RestTemplate获取JSON对象的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题:


  • 如何使用Spring RestTemplate映射JSON对象列表。

  • 如何映射嵌套的JSON对象。

我正在尝试使用https://bitpay.com/api/rates ,遵循的教程http://spring.io/guides/gs/consuming-rest/

I am trying to consume https://bitpay.com/api/rates, by following the tutorial from http://spring.io/guides/gs/consuming-rest/.

推荐答案

也许这样。 ..

ResponseEntity<Object[]> responseEntity = restTemplate.getForEntity(urlGETList, Object[].class);
Object[] objects = responseEntity.getBody();
MediaType contentType = responseEntity.getHeaders().getContentType();
HttpStatus statusCode = responseEntity.getStatusCode();

RequestMapping的控制器代码

@RequestMapping(value="/Object/getList/", method=RequestMethod.GET)
public @ResponseBody List<Object> findAllObjects() {

    List<Object> objects = new ArrayList<Object>();
    return objects;
}

ResponseEntity 是一个扩展 HttpEntity ,它会添加 HttpStatus 状态代码。用于 RestTemplate 以及 @Controller 方法。
RestTemplate 此类由 getForEntity()返回()返回

ResponseEntity is an extension of HttpEntity that adds a HttpStatus status code. Used in RestTemplate as well @Controller methods. In RestTemplate this class is returned by getForEntity() and exchange().

这篇关于使用Spring RestTemplate获取JSON对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆