博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在Java客户端调用RESTful服务
阅读量:5118 次
发布时间:2019-06-13

本文共 2464 字,大约阅读时间需要 8 分钟。

在这个例子中,我们将看到如何使用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();

                }

              } }

运行后输出结果是:

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();

               }

              }      }

转载于:https://www.cnblogs.com/firstdream/p/10038690.html

你可能感兴趣的文章
kafka简介及集群部署
查看>>
Mysql 安装时Mariadb冲突问题
查看>>
java IO流 之 字符流
查看>>
centos7安装nodejs
查看>>
笔记一 air android开发简介
查看>>
7月19号=》231页-240页
查看>>
eclipse svn知识
查看>>
IOS之KVC全解
查看>>
安卓开发:UI组件-布局管理器和文本显示
查看>>
WdatePicker日历添加事件,在任意月改变时处理日期事件
查看>>
iframe自动高度
查看>>
Swoole + zphp 改造为专门用来开发接口的框架
查看>>
Mysql 5.7.17安装后登录mysql的教程方法
查看>>
randint模块无法加载
查看>>
yarn-site.xml的配置内容
查看>>
MySQL Innodb事务自动提交(autocommit)
查看>>
how does vector work?
查看>>
简单有趣的二维码
查看>>
HOJ-1008
查看>>
教你如何用青花瓷(Charles)抓包
查看>>