My current code:
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>
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?
[edit]
Is it correct?
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();
[added]
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.
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.
question from:
https://stackoverflow.com/questions/32623407/add-my-custom-http-header-to-spring-resttemplate-request-extend-resttemplate 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…