Reputation: 547
Using Spring RESTTeample how do I pass hashmap values for url on post?
I am trying to use Spring RESTTeample to post a User Object to a web service but the issue I am having is that I am putting my id into a hashMap and I dont know how to pass the hashmap into RESTTemplate to use. Can you please look at the following code and let me know.. I dont want to hard code the ID on the URL
Map<String, String> vars = new HashMap<String, String>();
vars.put("id", "MMS");
RestTemplate rt = new RestTemplate();
rt.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
rt.getMessageConverters().add(new StringHttpMessageConverter());
URI uri = new URI("http://" + mRESTServer.getHost() + ":8080/springmvc-resttemplate-test/api/{id}");
User u = new User();
u.setName("Mickey Mouse");
u.setUser("MMS");
User returns = rt.postForObject(uri, u, User.class);
Upvotes: 0
Views: 1073
Reputation: 5739
In the code given, you are currently only passing the user information. If you want to pass the id and the user information to the REST service, why not put the user object into the hashmap along with the id and pass the hashmap to the rest service. A sample will be like this
Map<String, Object> request = new HashMap<String, Object>();
request.put("id", "MMS");
request.put("user", user);
restTemplate.postForObject(completeUrl,request, User.class);
This is provided your rest service accepts such an input.
Upvotes: 1