java客户端调用restful接口

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:  \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客户端调用restful接口的主要内容,如果未能解决你的问题,请参考以下文章

java rest api接口 怎么保证安全性

java调用rest接口 maven需要写哪些依赖

现在有一个SSO的Restful 的接口,用java代码具体怎么调用这个接口啊,需要传参啊?

java后台接口怎么方便返回restful数据

webservice 和 RESTful API 接口调用

Java 调用SAP PO 的Rest接口