日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java中的简单REST客户端

發布時間:2023/12/3 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java中的简单REST客户端 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如今,大多數用于與某些服務器通信的移動應用程序都使用REST服務。 這些服務也是與JavaScript或jQuery一起使用的常見做法。 現在,我知道在Java中為REST服務創建客戶端的2種方法,在本文中,我將嘗試演示這兩種方法,希望它們能以某種方式對某人有所幫助。

1.使用Apache HttpClient

Apache HttpClient庫簡化了HTTP請求的處理。 要使用此庫,您必須從其網站下載具有相關性的二進制文件。
這是HTTP GET方法的代碼:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; public class Test {public static void main(String[] args) throws ClientProtocolException, IOException {HttpClient client = new DefaultHttpClient();HttpGet request = new HttpGet('http://restUrl');HttpResponse response = client.execute(request);BufferedReader rd = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));String line = '';while ((line = rd.readLine()) != null) {System.out.println(line);}} }

對于Post方法; 用于在帖子中發送簡單的字符串:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; public class Test {public static void main(String[] args) throws ClientProtocolException, IOException {HttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost('http://restUrl');StringEntity input = new StringEntity('product');post.setEntity(input);HttpResponse response = client.execute(post);BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));String line = '';while ((line = rd.readLine()) != null) {System.out.println(line);}} }

您還可以通過以下方式發送POJO的完整JSON或XML :將表示JSON或XML的String用作StringEntity的參數,然后設置輸入內容類型。 像這樣:

StringEntity input = new StringEntity('{\'name1\':\'value1\',\'name2\':\'value2\'}'); //here instead of JSON you can also have XML input.setContentType('application/json');

對于JSON,您可以使用JSONObject創建JSON的字符串表示形式。

JSONObject json = new JSONObject(); json.put('name1', 'value1'); json.put('name2', 'value2'); StringEntity se = new StringEntity( json.toString());

并在后請求中發送多個參數:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; public class Test {public static void main(String[] args) throws ClientProtocolException, IOException {HttpClient client = new DefaultHttpClient();HttpPost post = new HttpPost('http://restUrl');List nameValuePairs = new ArrayList(1);nameValuePairs.add(new BasicNameValuePair('name', 'value')); //you can as many name value pair as you want in the list.post.setEntity(new UrlEncodedFormEntity(nameValuePairs));HttpResponse response = client.execute(post);BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));String line = '';while ((line = rd.readLine()) != null) {System.out.println(line);}} }

2.使用球衣

Jersey是JSR-311規范(Java中REST支持的規范)的參考實現。 澤西島基本上包含一個REST服務器和一個REST客戶端。 它提供了一個庫來與產生REST服務的服務器通信。 對于http get方法:

import java.io.IOException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import org.apache.http.client.ClientProtocolException; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; public class Test {public static void main(String[] args) throws ClientProtocolException, IOException {ClientConfig config = new DefaultClientConfig();Client client = Client.create(config);WebResource service = client.resource(UriBuilder.fromUri('http://restUrl').build());// getting XML dataSystem.out.println(service. path('restPath').path('resourcePath').accept(MediaType.APPLICATION_JSON).get(String.class));// getting JSON dataSystem.out.println(service. path('restPath').path('resourcePath').accept(MediaType.APPLICATION_XML).get(String.class));} }

您還可以通過其他媒體格式獲得響應,例如PLAIN或HTML。
對于HTTP POST方法:

import java.io.IOException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriBuilder; import org.apache.http.client.ClientProtocolException; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.core.util.MultivaluedMapImpl; public class Test {public static void main(String[] args) throws ClientProtocolException, IOException {ClientConfig config = new DefaultClientConfig();Client client = Client.create(config);WebResource webResource = client.resource(UriBuilder.fromUri('http://restUrl').build());MultivaluedMap formData = new MultivaluedMapImpl();formData.add('name1', 'val1');formData.add('name2', 'val2');ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientResponse.class, formData);System.out.println('Response ' + response.getEntity(String.class));} }

如果您在POST中使用POJO,則可以執行以下操作:

ClientResponse response = webResource.path('restPath').path('resourcePath'). type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, myPojo); System.out.println('Response ' + response.getEntity(String.class));

這里的myPojo是自定義POJO類的實例。
您還可以使用Jersey的Form類在POST請求中提交多個參數:

import java.io.IOException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import org.apache.http.client.ClientProtocolException; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.api.representation.Form; public class Test {public static void main(String[] args) throws ClientProtocolException, IOException {ClientConfig config = new DefaultClientConfig();Client client = Client.create(config);WebResource service = client.resource(UriBuilder.fromUri('http://restUrl').build());Form form = new Form();form.add('name1', 'value1');form.add('name2', 'value1');ClientResponse response = service.path('restPath').path('resourcePath').type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, form);System.out.println('Response ' + response.getEntity(String.class));} }

祝您編程愉快,別忘了分享!

參考:來自harryjoy博客的JCG合作伙伴 Harsh Raval的Java中的簡單REST客戶端 。


翻譯自: https://www.javacodegeeks.com/2012/09/simple-rest-client-in-java.html

總結

以上是生活随笔為你收集整理的Java中的简单REST客户端的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。