简体   繁体   中英

How to consume RESTful web-service?

In this tutorial written how to create REST service and how to consume it. I confused by consuming example. There we need to have on client side jersey.jar and write like this:

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

Why client need to know how web-service implemented(jersey or may be ohter implementation)? Why client side don't consume it by using simple InputStream ?

In this particular tutorial you are using the jersey CLIENT to interact with a RESTful Service.

You could also just interact with the service directly by just manually creating an HTTP request and receiving the response and parsing accordingly(http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html).

The Jersey client is ultimately is just an abstraction of this to make it easier to work with.

    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();
        }

Simplest way to consume Restful web services is using Spring RestTemplate. http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/client/RestTemplate.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM