日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

restful java客户端_如何在Java客户端调用RESTful服务

發(fā)布時間:2024/4/18 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 restful java客户端_如何在Java客户端调用RESTful服务 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在這個例子中,我們將看到如何使用java.net包實用工具,創(chuàng)建一個訪問REST服務(wù)RESTful的客戶端。當(dāng)然這不是創(chuàng)建一個RESTful客戶端最簡單的方法,因為你必須自己讀取服務(wù)器端的響應(yīng),以及Json和Java對象的轉(zhuǎn)換。

請求Get

public class JavaNetURLRESTFulClient {

private static final String targetURL = "http://localhost:8080/JerseyJSONExample/rest/jsonServices/print/Jamie";

public static void main(String[] args) {

try {

URL restServiceURL = new URL(targetURL);

HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection(); ?????? ?????? ?????? httpConnection.setRequestMethod("GET"); ?????? ?????? ?????? httpConnection.setRequestProperty("Accept", "application/json");

if (httpConnection.getResponseCode() != 200) { ?????? ?????? ?????? ?????? throw new RuntimeException("HTTP GET Request Failed with Error code : " ?????? ?????? ?????? ?????? ?????? ?????? + httpConnection.getResponseCode()); ?????? ?????? ?????? }

BufferedReader responseBuffer = new BufferedReader(new InputStreamReader( ?????? ?????? ?????? ?????? (httpConnection.getInputStream())));

String output; ?????? ?????? ?????? System.out.println("Output from Server:? \n");

while ((output = responseBuffer.readLine()) != null) { ?????? ?????? ?????? ?????? System.out.println(output); ?????? ?????? ?????? }

httpConnection.disconnect();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

} }

運行后輸出結(jié)果是:

Output from Server: {"id":1,"firstName":"Jamie","age":22,"lastName":"Diaz"}

POST提交:

public class JavaNetURLRESTFulClient {

private static final String targetURL = "http://localhost:8080/JerseyJSONExample/rest/jsonServices/send";

public static void main(String[] args) {

try {

URL targetUrl = new URL(targetURL);

HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection(); ?????? ?????? ?????? httpConnection.setDoOutput(true); ?????? ?????? ?????? httpConnection.setRequestMethod("POST"); ?????? ?????? ?????? httpConnection.setRequestProperty("Content-Type", "application/json");

String input = "{\"id\":1,\"firstName\":\"Liam\",\"age\":22,\"lastName\":\"Marco\"}";

OutputStream outputStream = httpConnection.getOutputStream(); ?????? ?????? ?????? outputStream.write(input.getBytes()); ?????? ?????? ?????? outputStream.flush();

if (httpConnection.getResponseCode() != 200) { ?????? ?????? ?????? ?????? throw new RuntimeException("Failed : HTTP error code : " ?????? ?????? ?????? ?????? ?????? + httpConnection.getResponseCode()); ?????? ?????? ?????? }

BufferedReader responseBuffer = new BufferedReader(new InputStreamReader( ?????? ?????? ?????? ?????? ?????? (httpConnection.getInputStream())));

String output; ?????? ?????? ?????? System.out.println("Output from Server:\n"); ?????? ?????? ?????? while ((output = responseBuffer.readLine()) != null) { ?????? ?????? ?????? ?????? System.out.println(output); ?????? ?????? ?????? }

httpConnection.disconnect();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}????? }

總結(jié)

以上是生活随笔為你收集整理的restful java客户端_如何在Java客户端调用RESTful服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。