开发者

How do I post JSON data using Spring's RestTemplate?

I don't quite understand how to solve my particular situation given the examples I have found. I am trying to post a JSON string to a URL to create a new object. The response from the REST Service is a URI to the newly creat开发者_JAVA技巧ed resource. The REST call is suppose to look like this:

http://www.some.url.com/REST/create?data={ "param1":"value1", "param2":"value2", ... }

So what are my parameters based upon the example above? Is it something like this?

RestTemplate restTemplate = new RestTemplate();
URI uri = restTemplate.postForLocation("http://www.some.url.com/REST/create?data=", "{ "param1":"value1", "param2":"value2", ... }");

I currently have all the param/value pairs in a Map which can be easily transformed to JSON using Jackson. In this case can I do the following:

Map<String, String> record = new HashMap<String, String>();
record.put("param1","value1");
record.put("param2","value2");

URI uri = restTemplate.postForLocation("http://www.some.url.com/REST/create?data=", record);

Any help would be greatly appreciated!


easy way to this this

public static String callService(String url, Map<String, String> data) throws Exception 
{RestTemplate rest = new RestTemplate();
ResponseEntity<String> response= rest.postForEntity(url, data, String.class);
            System.out.println(response.getBody()); 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜