10

I have a rest api url and submitted the same as POST request with body (user name, password, other parameters) via Rest Client (restclient-ui-2.4-jar-with-dependencies) and it got worked fine without any issues.

Ex:

URL: https://test.com/cgi-bin/testing/api Body: username=testuser&password=pass123&id=13002&name=raju

The same is not working fine when i used Spring RestTemplate postForObject(url, varmap, Employee.class) method.

Can someone help me with a simple example where the request is a URL, with body parameters and the response is XML which is mapped with a class?

Sample Code:

  MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
  map.add("username", "test");
  map.add("password", "test123");
  map.add("id", "1234");
  MarshallingHttpMessageConverter mc = new MarshallingHttpMessageConverter();
  mc.setMarshaller(new Jaxb2Marshaller());
  mc.setUnmarshaller(new Jaxb2Marshaller());
  list.add(marshallingHttpMessageConverter);
  emediateRestTemplate.setMessageConverters(list);
  Employee employee = (Employee) restTemplate.postForObject(url, map, Employee.class);

Thanks in advance, Kathir

Kathir
  • 2,733
  • 12
  • 39
  • 67
  • Can you show your code that is not working? – nicholas.hauschild Aug 30 '12 at 18:23
  • restTemplate.postForObject(url, parametersMap, Employee.class); . Also could you please let me know the differences between postForObject vs postForExchange? – Kathir Aug 30 '12 at 18:35
  • You should update the question with code, not add it in comments. Also, please show more than a single line of code, show the relevant setup too. Finally, there is a wealth of documentation on Spring's site about what each method does. Perhaps you want to take a look at that first. – nicholas.hauschild Aug 30 '12 at 18:39
  • I've updated the question. could you please help me with the code as well as let me know the Spring site where examples are available for each method? – Kathir Aug 30 '12 at 18:44
  • http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html – nicholas.hauschild Aug 30 '12 at 18:45
  • now, please let me know your views on the above code or anything suspicious? – Kathir Aug 30 '12 at 18:46

1 Answers1

15

The above converters Ex: "MarshallingHttpMessageConverter" are not required.

MultiValueMap<String, String> parametersMap = new LinkedMultiValueMap<String, String>();
parametersMap.add("username", "test");
parametersMap.add("password", "test123");
parametersMap.add("id", "1234");

For Post:

restTemplate.postForObject(url, parametersMap, Employee.class);
  • url is String - rest api URL
  • parametersMap - MultiValueMap
  • Employee - object which needs to be converted from the JSON response

For Get:

restTemplate.getForObject(url,  class object, variablesMap);
  • url is : String - rest api URL
  • variablesMap - Map
  • class object - object which needs to be converted from the JSON response
payne
  • 4,691
  • 8
  • 37
  • 85
Kathir
  • 2,733
  • 12
  • 39
  • 67