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

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

问题描述

我有两个问题:

  • 如何使用 Spring RestTemplate 映射 JSON 对象列表.
  • 如何映射嵌套的 JSON 对象.

我正在尝试按照 https://bitpay.com/api/rates 的教程使用http://spring.io/guides/gs/sumption-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;
}

ResponseEntityHttpEntity 的扩展,它添加了一个 HttpStatus 状态代码.在 RestTemplate 以及 @Controller 方法中使用.在 RestTemplate 中,这个类由 getForEntity()exchange() 返回.

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天全站免登陆