igx igx - 1 year ago 169
Java Question

not getting headers passed with RestTemplate.getForObject

I am newbie using Spring . I am working on a code trying to pass headers using restTemplate.getForObject

client side :

String userServiceUrl = "http://localhost:8080/SampleRest/api/user/";
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", UUID.randomUUID().toString() );
restTemplate.getForObject(userServiceUrl + "{id}",User.class,HttpMethod.GET,headers) ;

however on server the request header "Authentication" is not passed at all . there is no header "Authentication"

requestHeaders.get("Authorization").get(0) //yields null exception

I cannot use restTemplate.exchange

what am I doing wrong ?

Help will greatly appreciated

jny jny
Answer Source

There is no option to pass headers in getForObject method of restTemplate.

You can implement ClientHttpRequestInterceptor to set the headers if you don't want to use exchange. You can also overwrite SimpleClientHttpRequestFactory