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

本文共 2409 字,大约阅读时间需要 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();

               }

              }      }

转载地址:http://okael.baihongyu.com/

你可能感兴趣的文章
windows 操作系统原版下载地址
查看>>
剑指offer——O(1)时间删除单链表节点
查看>>
OSPF在企业网络中的应用
查看>>
什么是NIO(转载)
查看>>
第五课 SCCM2012通过OSD功能实现操作系统部署(上)
查看>>
易宝典文章——用ISA 2006标准版发布Exchange 2010的OWA系列之生成Exchange证书申请文件...
查看>>
shell 读取键盘输入
查看>>
Linux静态库和动态库
查看>>
Spring中管理Bean依赖注入之后和Bean销毁之前的行为
查看>>
vmware 虚拟机网络
查看>>
JAVA基本程序设计结构
查看>>
UNIX 高手的 10 个习惯
查看>>
Basics of Memory Addresses in C
查看>>
加密解密
查看>>
AIX 卷组管理
查看>>
vsftpd基于本地用户和mysql认证配置
查看>>
动态代理简单测试
查看>>
Linux下切分Tomcat的catalina.out日志文件
查看>>
XSS***的介绍及防御
查看>>
《冈仁波齐》让我看到的不止是信仰,还有不忘初心
查看>>