zoukankan      html  css  js  c++  java
  • 如何在Java客户端调用RESTful服务

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

                         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: ");                      while ((output = responseBuffer.readLine()) != null) {                             System.out.println(output);                      }

                         httpConnection.disconnect();

                    } catch (MalformedURLException e) {

                         e.printStackTrace();

                    } catch (IOException e) {

                         e.printStackTrace();

                   }

                  }      }

  • 相关阅读:
    冒泡排序法
    冒泡排序法
    【HAOI2008】圆上的整点
    2018年全国多校算法寒假训练营练习比赛(第四场)F:Call to your teacher
    (java)Jsoup爬虫学习--获取网页所有的图片,链接和其他信息,并检查url和文本信息
    CSS 选择器
    (java)selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出
    (java)selenium webdriver学习---三种等待时间方法:显式等待,隐式等待,强制等待
    使用D3绘制图表(5)--水平柱状图表
    使用D3绘制图表(4)--面积图表
  • 原文地址:https://www.cnblogs.com/firstdream/p/10038690.html
Copyright © 2011-2022 走看看