我有一个RESTful API I 'm trying to connect with via Android and RestTemplate. All requests to the API are authenticated with HTTP Authentication, through setting the headers of the HttpEntity and then using RestTemplate' s exchange()
方法 .
所有GET请求都以这种方式工作,但我无法弄清楚如何完成经过身份验证的POST请求 . postForObject
和 postForEntity
处理POST,但没有简单的方法来设置身份验证标头 .
因此对于GET来说,这很有用:
HttpAuthentication httpAuthentication = new HttpBasicAuthentication("username", "password");
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAuthorization(httpAuthentication);
HttpEntity<?> httpEntity = new HttpEntity<Object>(requestHeaders);
MyModel[] models = restTemplate.exchange("/api/url", HttpMethod.GET, httpEntity, MyModel[].class);
但POST显然不能与 exchange()
一起使用,因为它从不发送自定义标头,我也看不到如何使用 exchange()
设置请求体 .
从RestTemplate进行经过身份验证的POST请求的最简单方法是什么?
4 回答
好的找到了答案 .
exchange()
是最好的方法 . 奇怪的是HttpEntity
类没有setBody()
方法(它有getBody()
),但仍然可以通过构造函数设置请求体 .略有不同的方法:
当我在尝试从Java进行REST调用时尝试通过身份验证时,我最近处理了一个问题,虽然这个线程(和其他线程)的答案有所帮助,但仍然有一些试验和错误涉及到它工作 .
对我有用的是在
Base64
中编码凭据并将它们添加为基本授权标头 . 然后我将它们作为HttpEntity
添加到restTemplate.postForEntity
,这给了我所需的响应 .这是我为此完整编写的类(扩展RestTemplate):
非常有用我有一个稍微不同的场景,我请求xml本身是POST的主体,而不是一个参数 . 为此,可以使用以下代码 - 发布作为答案,以防其他有类似问题的人将受益 .