Search code examples
javaglassfishjerseyjersey-client

Using Jersey For Gathering Information from RESTful API


I am trying to get information from a REST API in a Java program. It was recommended to me that I should use Jersey to do this.

From what I have read so far, Jersey seems to have two functions. The first is to create a REST API and the other is to gather information from a REST API. I am using an already created REST API, so I do not need the first function.

So I am looking for examples on how to do a simple GET request using Jersey. However, every example I have found so far seems to be how to create your API to use GET or have been using a different version of Jersey (I am using Jersey with Glassfish). I am struggling finding any information how to do this.

I have been using the following: https://jersey.java.net/download.html.

And I have been trying to follow the following documentation (User Guide): https://jersey.java.net/nonav/documentation/latest/user-guide.html#d0e1290

I would just like to know how to do a GET request using Jersey. Any information on this or links to documentation / examples would be great.

Thanks ahead of time.


Solution

  • You want to build a REST client, not a REST service.
    And you're in the right documentation, but the wrong chapter. What you're looking for is 5.3 Overview of the Client API.
    Basically, you will use four pieces.

    1. Client
    2. WebTarget
    3. Invocation.Builder
    4. Response

    A complete code example is in section 5.3.6.
    To start, ignore the ClientConfig. Instantiate the simplest client using Client client = ClientBuilder.newClient(); and then focus on setting up your WebTarget and Invocation.Builder.