1

I'd like to use RestTemplate to obtain response from server and process that response in my Android app, but server answers with prefix (or variable) in json body, so response looks similar to this:

response={"foo":"bar"}

Is it possible to omit that "response=" part som simple way, or do I need to reimplement MappingJacksonHttpMessageConverter class?

Thanks in advance

Edit: It works now, following code is based on newest SpringAndroid (1.0.0 RELEASE). RestTEplate(true) constructor adds required convertors and request.toMap() builds a MultiValueMap, which is only body type FormHttpMessageConverter accepts.

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(new MediaType("application", "x-www-form-urlencoded"));

RestTemplate restTemplate = new RestTemplate(true);
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());

final String url = "http://www.dummy.org/herp/getDerp";
String result = restTemplate.postForObject(url, request.toMap(), String.class);

Now I have string on output, from which I can extract JSON and parse that.

shmoula
  • 1,053
  • 2
  • 10
  • 33
  • Do you mind posting the code you're using to obtain the response? – dardo Jun 04 '12 at 16:51
  • Just added piece of code, but I think it's not important. – shmoula Jun 04 '12 at 20:54
  • Wouldn't you just parse the response with a jackson mapper? – dardo Jun 04 '12 at 21:12
  • I think it can parse messages like {"foo":"bar"}, not response={"foo":"bar"} – shmoula Jun 06 '12 at 11:04
  • Right, I'm just wondering how you know there is a response={"foo":"bar"} is it part of the result in the body, if so, how is it being stored, etc. – dardo Jun 06 '12 at 12:49
  • Server side is not mine, so I cant modify it. And yes, response={"foo":"bar"} is whole response body. – shmoula Jun 07 '12 at 16:23
  • What fields are within CheckLoginResponse, does it have a field for...well...foo in this case. When it maps the object, the field names in the json should match the fields in the pojo. – dardo Jun 07 '12 at 18:20
  • Thank you, dardo, it works now - please see edited question. My problem was not resposne, but request - FormHttpMessageConverter does not accept anything, but MultiValueMap. – shmoula Jun 09 '12 at 12:45

0 Answers0