1

鉴于我找到的示例,我不太明白如何解决我的特殊情况。我正在尝试将 JSON 字符串发布到 URL 以创建新对象。来自 REST 服务的响应是新创建资源的 URI。REST 调用假设如下所示:

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

那么根据上面的例子,我的参数是什么?是这样的吗?

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

我目前在 Map 中拥有所有参数/值对,可以使用 Jackson 轻松将其转换为 JSON。在这种情况下,我可以执行以下操作:

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);

任何帮助将不胜感激!

4

1 回答 1

0

简单的方法

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()); 
}
于 2014-02-02T01:37:43.610 回答