0

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?

1
  • As mentioned in the same tutorial, "Jersey contains a REST client library which can be used for testing or to build a real client in Java. Alternative you could use Apache HttpClient to create a client."
    – mazaneicha
    Jul 4, 2012 at 15:23

3 Answers 3

1

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.

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.