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

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.

+20
source share
2 answers

You can pass custom http headers using the RestTemplate exchange method, as shown 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 to call the leisure 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(); 
+26
source

Add the "User-Agent" header to your request.

Some servers try to block spiders and scrapers from accessing their server, because in earlier days the requests did not send the user agent header.

You can either try to set the user agent user value or use some value that the browser identifies, for example, "Mozilla / 5.0 Firefox / 26.0".

 RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); headers.setContentType(MediaType.APPLICATION_JSON); headers.add("user-agent", "Mozilla/5.0 Firefox/26.0"); headers.set("user-key", "your-password-123"); // optional - in case you auth in headers HttpEntity<String> entity = new HttpEntity<String>("parameters", headers); ResponseEntity<Game[]> respEntity = restTemplate.exchange(url, HttpMethod.GET, entity, Game[].class); logger.info(respEntity.toString()); 
0
source

All Articles