I’ve been using RestTemplate for a while and have always been satisfied !
But now It sucks.
I used to make my put like this :
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
restTemplate.put(URI, object);
Usually my server was accepting format in the URL in a Ruby way : resource.json
But now I’m consuming a server who is not doing so anymore. I need to use Header to deal with format.
Everything is ok, restTemplate set content-type to application/json but don’t set the Accept header. So my server is issuing a 406 error because default format is HTML, and he don’t render html.
So, does anybody has a workaround for putting both content and accept header to json and adding a json formatted body ?
You can get the headers from the RestTemplate object and add what you need to it (since the headers are nothing more than a map).
Here is the sample code provided by Dam after he got it working: