如何在Java中使用REST

如何在Java中使用REST,第1张

如何在Java中使用REST

工作示例,请尝试以下 *** 作

package restclient;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;public class NetClientGet {    public static void main(String[] args) {        try { URL url = new URL("http://localhost:3002/RestWebserviceDemo/rest/json/product/dynamicData?size=5");//your url i.e fetch data from . HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); if (conn.getResponseCode() != 200) {     throw new RuntimeException("Failed : HTTP Error pre : "  + conn.getResponseCode()); } InputStreamReader in = new InputStreamReader(conn.getInputStream()); BufferedReader br = new BufferedReader(in); String output; while ((output = br.readLine()) != null) {     System.out.println(output); } conn.disconnect();        } catch (Exception e) { System.out.println("Exception in NetClientGet:- " + e);        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5478304.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存