My current code is:
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 the form:
X-TP-DeviceID : <GUID>
What is the easiest way to do this in my case? Is there a way to add a custom header definition to my restTemplate object before sending a request to the server?
[edit]
It is right?
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 to work. However, I am not completely satisfied with this. In my case, I will need to provide the same custom headers for all my calls.
So, my next question is: can I automatically add my custom headers every time I call web-service , for example, by extending the RestTemplate class and placing all the custom headers there? Then all I would have to do is just use my own extended RestTemplate instead of the standard one, and all my custom headers will be present there by default.
source share