簡體   English   中英

如何使用RESTful Web服務?

[英]How to consume RESTful web-service?

教程中,編寫了如何創建REST服務以及如何使用它。 我被舉個例子弄糊塗了。 那里我們需要在客戶端jersey.jar編寫如下代碼:

Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());

為什么客戶需要知道Web服務的實現方式(球衣或以后的實現方式)? 為什么客戶端不使用簡單的InputStream來使用它?

在此特定教程中,您將使用Jersey CLIENT與RESTful服務進行交互。

您也可以通過手動創建HTTP請求並接收響應並進行相應的解析來直接與服務進行交互(http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html)。

最終,Jersey客戶端只是對此的一種抽象,以使其更易於使用。

    String URL ="http://localhost:8080/MyWServices/REST/WebService/";
    String ws_method_name = "getManagerListByRoleID";
    String WS_METHOD_PARAMS = "";

    HttpClient httpClient = new DefaultHttpClient();
    HttpContext httpContext = new BasicHttpContext();

    HttpGet httpGet = new HttpGet(URL + ws_method_name + WS_METHOD_PARAMS);
    String text = null;

    try {
        HttpResponse httpResponse = httpClient
                .execute(httpGet, httpContext);
        HttpEntity entity = httpResponse.getEntity();
        text = getASCIIContentFromEntity(entity);
        }catch(Exception e){
                e.printStackTrace();
        }

使用Restful Web服務的最簡單方法是使用Spring RestTemplate。 http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/client/RestTemplate.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM