restful java客户端_如何在Java客户端调用RESTful服务
在這個例子中,我們將看到如何使用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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中string的方法_java中
- 下一篇: java 如何检测死锁_Java如何查看