java客户端调用restful接口
2016-09-12 20:12
393 查看
在这个例子中,我们将看到如何使用java.net包实用工具,创建一个访问REST服务RESTful的客户端。当然这不是创建一个RESTful客户端最简单的方法,因为你必须自己读取服务器端的响应,以及Json和Java对象的转换。
请求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();
}
}
}
运行后输出结果是:
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();
}
}
}
请求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();
}
}
}
运行后输出结果是:
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();
}
}
}
相关文章推荐
- java util :获取国家省份城市工具类
- java之hashMap和hashTable的区别
- java 静态方法和单例模式的区别
- spring mvc实现restful
- leetCode练习(4)
- java基础02
- 各种排序算法的分析及java实现
- JAVA 方法定义及调用
- java util :获取国家省份城市工具类
- java实现冒泡排序
- Java 中的 scanner
- 用Java ac的第一道题。。PAT乙级1056 组合数的和
- Java笔记-快速失败and安全失败
- java集合和数组的排序总结
- java.security.KeyException 的解决方法
- java 抽象类实现接口是什么设计模式
- 快递鸟物流单号自动识别接口JAVA对接demo
- java4_第一章_对象导论
- 第一个Java程序示例——Hello World!
- rest token的一个实现