从RestTemplate获取标题数据和对象

时间:2015-10-28 21:44:18

标签: java json spring rest http-headers

我试图从响应标头中获取一些身份验证值,并获取已填充的对象。但我的问题是,我得到了标题值或填充对象,如下例所示。

RestTemplate restTemplate = new RestTemplate();
String requestJson = "{ \"identifier\": \"username\", \"password\": \"pw\" } ";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("KEY","apikey");
HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);

Login login = restTemplate.postForObject("https://url.com",entity, Login.class);

HttpEntity<String> response = template.exchange("https://url.com", HttpMethod.POST, entit, Login.class);

String resultString = response.getBody();
HttpHeaders headers = response.getHeaders();

有没有办法做响应=&gt;对象交换以后? 还是一种完全不同的解决方法?

1 个答案:

答案 0 :(得分:0)

我使用此代码解决了我的问题:

ResponseEntity<Login> response = restTemplate.exchange(url, HttpMethod.POST, request, Login.class);
Login login = response.getBody();
HttpHeaders headers = response.getHeaders();

所以我的结果是登录时填充的Login对象,我的Headers在标题中。