I am trying to post a json object to a .net web service:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
answer[] answers = restTemplate.postForObject(url, new Gson().toJson(request), answer[].class);
The generated json looks fine so far:
{"request":1234}
but when sent to the web service with the help of the restTemplate the content of the http request is kind of messed up:
"{\"request\":1234}"
and the service responds with error code 400 bad request
Edit: found the problem
The problem was that I encoded the object twice.
RestTemplate already encodes the object to json.
the working code is:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
answer[] answers = restTemplate.postForObject(url, request, answer[].class);
No need to encode the object with gson as RestTemplate already does that
correct code: