Java 从RestTemplate获取标题数据和对象
我试图从响应头中获取一些身份验证值,并获取填充的对象。但我的问题是,我要么得到标题值,要么得到填充对象,如这里的示例所示Java 从RestTemplate获取标题数据和对象,java,json,spring,rest,http-headers,Java,Json,Spring,Rest,Http Headers,我试图从响应头中获取一些身份验证值,并获取填充的对象。但我的问题是,我要么得到标题值,要么得到填充对象,如这里的示例所示 RestTemplate restTemplate = new RestTemplate(); String requestJson = "{ \"identifier\": \"username\", \"password\": \"pw\" } "; HttpHeaders headers = new HttpHeaders(); headers.setContentT
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);
RestTemplate RestTemplate=new RestTemplate();
String requestJson=“{\'标识符\':\'用户名\',\'密码\':\'密码\':\'密码\'”;
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
标题。添加(“键”、“apikey”);
HttpEntity=新的HttpEntity(requestJson,headers);
Login Login=restTemplate.postForObject(“https://url.com“,实体,Login.class);
或
HttpEntity response=template.exchange(“https://url.com,HttpMethod.POST,entit,Login.class);
字符串resultString=response.getBody();
HttpHeaders=response.getHeaders();
有没有办法在以后进行response=>对象交换?
还是一种完全不同的解决方法?我使用以下代码解决了我的问题:
ResponseEntity<Login> response = restTemplate.exchange(url, HttpMethod.POST, request, Login.class);
Login login = response.getBody();
HttpHeaders headers = response.getHeaders();
ResponseEntity response=restemplate.exchange(url、HttpMethod.POST、请求、Login.class);
Login Login=response.getBody();
HttpHeaders=response.getHeaders();
因此,我的结果是Login中填充的Login对象,我的头在头中
ResponseEntity<Login> response = restTemplate.exchange(url, HttpMethod.POST, request, Login.class);
Login login = response.getBody();
HttpHeaders headers = response.getHeaders();