将我的自定义http标头添加到Spring RestTemplate请求中/扩展RestTemplate [英] Add my custom http header to Spring RestTemplate request / extend RestTemplate

查看:233
本文介绍了将我的自定义http标头添加到Spring RestTemplate请求中/扩展RestTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的代码:

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Mall[] malls = restTemplate.getForObject(url, Mall[].class);

我需要为请求添加一些自定义标头,格式为:

I need to add some custom headers for my request, in form:

X-TP-DeviceID : <GUID>

在我的情况下,最简单的方法是什么?在将请求发送到服务器之前,是否可以将自定义标头定义添加到我的restTemplate对象?

What is the simplest way to do that in my case? Is there any way to add custom headers definition to my restTemplate object before I send the request to server?

对吗?

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

HttpHeaders headers = new HttpHeaders();
headers.set("X-TP-DeviceID", "1234567890");
HttpEntity entity = new HttpEntity(headers);

HttpEntity<Mall[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, Mall[].class);

Mall[] malls = response.getBody();

[添加]

因此,我设法使其正常运行.但是,我对此并不完全满意.就我而言,我需要为我进行的所有调用提供相同的自定义标头.

So, I managed to get it working. However, I'm not fully satisfied with that. In my case I will need to provide the same custom headers for all the calls I make.

所以,我的下一个问题是-是否可以将我的自定义标头设置为在每个web-service调用中自动添加,例如,通过扩展RestTemplate类并将所有自定义标头放在那里?然后,我所需要做的就是简单地使用我的自定义扩展RestTemplate而不是普通的扩展名,并且默认情况下,我的所有自定义标头都将出现在这里.

So, my next question is - Is it possible to set my custom headers to be added automatically on each web-service call, for example, by extending RestTemplate class and putting all custom headers there? Then, all I would be needing to do would be to simply use my custom extended RestTemplate instead of the stock one, and all my custom headers will be present there by default.

推荐答案

您可以使用RestTemplate交换方法传递自定义的HTTP标头,如下所示.

You can pass custom http headers with RestTemplate exchange method as below.

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("X-TP-DeviceID", "your value");

HttpEntity<RestRequest> entityReq = new HttpEntity<RestRequest>(request, headers);

RestTemplate template = new RestTemplate();

ResponseEntity<RestResponse> respEntity = template
    .exchange("RestSvcUrl", HttpMethod.POST, entityReq, RestResponse.class);

以下是更新的代码. 此链接提供了几种通过示例调用休息服务的方法

EDIT : Below is the updated code. This link has several ways of calling rest service with examples

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("X-TP-DeviceID", "your value");

HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

ResponseEntity<Mall[]> respEntity = restTemplate.exchange(url, HttpMethod.POST, entity, Mall[].class);

Mall[] resp = respEntity.getBody();

这篇关于将我的自定义http标头添加到Spring RestTemplate请求中/扩展RestTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆