我有两个问题:
如何使用Spring RestTemplate映射JSON对象列表。 如何映射嵌套的JSON对象。
我试图消费https://bitpay.com/api/rates,从http://spring.io/guides/gs/consuming-rest/遵循教程。
我有两个问题:
如何使用Spring RestTemplate映射JSON对象列表。 如何映射嵌套的JSON对象。
我试图消费https://bitpay.com/api/rates,从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()和exchange()返回。
其他回答
作为一个通用模块,Page<?>对象可以被模块反序列化,就像JodaModule, Log4jJsonModule等。参考我的回答。使用Pageable字段测试端点时出现JsonMappingException
我从这个帖子https://jira.spring.io/browse/SPR-8263找到了工作。
基于这篇文章,你可以返回一个类似这样的列表:
ResponseEntity<? extends ArrayList<User>> responseEntity = restTemplate.getForEntity(restEndPointUrl, (Class<? extends ArrayList<User>>)ArrayList.class, userId);
这里提到了3种检索对象列表的方法。所有这些都将完美地工作
@RequestMapping(value = "/emp2", produces = "application/json")
public List<Employee> getEmp2()
{
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
ResponseEntity<List<Employee>> response = restTemplate.exchange(
"http://hello-server/rest/employees", HttpMethod.GET,entity,
new ParameterizedTypeReference<List<Employee>>() {});
return response.getBody();
}
(或)
@RequestMapping(value = "/emp3", produces = "application/json")
public List<Employee> getEmp3()
{
Employee[] empArray = restTemplate.getForObject("http://hello-server/rest/employees", Employee[].class);
List<Employee> emp= Arrays.asList(empArray);
return emp;
}
(或)
@RequestMapping(value = "/emp4", produces = "application/json")
public Employee[] getEmp4()
{
ResponseEntity<Employee[]> responseEntity = restTemplate.getForEntity("http://hello-server/rest/employees", Employee[].class);
Employee[] empList = responseEntity.getBody();
//MediaType contentType = responseEntity.getHeaders().getContentType();
//HttpStatus statusCode = responseEntity.getStatusCode();
return empList;
}
Employee.class
public class Employee {
private Integer id;
private String name;
private String Designation;
private String company;
//getter setters and toString()
}
对我来说这很有效
Object[] forNow = template.getForObject("URL", Object[].class);
searchList= Arrays.asList(forNow);
Object是你想要的类的位置
你可以为每个条目创建POJO,
class BitPay{
private String code;
private String name;
private double rate;
}
然后使用BitPay列表的ParameterizedTypeReference,你可以使用:
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<List<Employee>> response = restTemplate.exchange(
"https://bitpay.com/api/rates",
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<BitPay>>(){});
List<Employee> employees = response.getBody();