我正在尝试用 Java 调用 Rest 服务。我是网络和休息服务的新手。我有 Rest 服务,它返回 JSON 作为响应。我有以下代码,但我认为它不完整,因为我不知道如何使用 JSON 处理输出。
public static void main(String[] args) {
try {
URL url = new URL("http://example.com:7000/test/db-api/processor");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "application/json");
OutputStream os = connection.getOutputStream();
//how do I get json object and print it as string
os.flush();
connection.getResponseCode();
connection.disconnect();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
我是 Rest 服务和 JSON 的新手。
原文由 Umesh K 发布,翻译遵循 CC BY-SA 4.0 许可协议
因为这是一个
PUT
请求你在这里遗漏了一些东西:看看 这个 可以更清楚地了解如何访问 web 服务。