Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add my custom http header to Spring RestTemplate request / extend RestTemplate

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.

like image 469
user1209216 Avatar asked Sep 17 '15 05:09

user1209216


People also ask

How do I set HTTP header in RestTemplate?

I suggest using one of the exchange methods that accepts an HttpEntity for which you can also set the HttpHeaders . (You can also specify the HTTP method you want to use.) For example, RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.

How do I add a custom header in HTTP spring boot?

To set the custom header to each response, use addHeader() method of the HttpServletResponse interface. That's all about setting a header to all responses in Spring Boot.

How do you pass the header in RestTemplate getForObject?

The RestTemplate getForObject() method does not support setting headers. The solution is to use the exchange() method. HttpHeaders headers = new HttpHeaders(); headers. set("Header", "value"); headers.

How do you pass the headers on a RestTemplate call?

Instead of setting headers by using dedicated methods (like setAccept in previous code), you can use general set (headerName, headerValue) method. headers. set("Accept", "application/json");


1 Answers

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(); 
like image 122
kswaughs Avatar answered Oct 08 '22 02:10

kswaughs