[英]Spring Rest - set headers without request body
I need to set an authorization header in the REST call in my client, but the web service doesn't expect a request body. 我需要在客户端的REST调用中设置授权标头,但Web服务不希望有请求正文。 How would I send this? 我将如何发送?
Right now I have this, which I know is wrong: 现在我有这个,我知道这是错误的:
HttpEntity<String> response = new HttpEntity<String>(authToken, headers);
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
TransactionResponse transactionResponse = restTemplate.postForObject(uri, response, TransactionResponse.class);
return transactionResponse;
To add the authentication header, can't you just add it to the headers? 要添加身份验证标头,是否不能仅将其添加到标头中?
headers.add("Authentication", authToken);
I think (but have not tried) that you could give the type parameter for the entity as Void
: 我认为(但尚未尝试过)您可以将实体的类型参数指定为Void
:
HttpEntity<Void> response = new HttpEntity<>(null, headers);
Bear in mind that the actual header name needed might be different from the one I used above. 请记住,所需的实际标题名称可能与我上面使用的名称不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.