Search code examples
javarestjersey-2.0jersey-client

Jersey 2 - Simple Client Get with Headers Example


I'm trying to do a simple get with headers using Jersey2. I read through Jersey 2.x: How to add Headers on RESTful Client to help create my code below, but I'm not having much luck. I created the clientRequestFilter as suggested, but don't know how to register it with the client properly, which I think may be my core issue.

I'm hoping to get a sanity check to see what I'm doing wrong need it be registering the ClientRequestFilter or if I'm missing more than that. Below is the documentation of the webservice I'm trying to consume and my jersey2 client code to try to consume it.

Web Service Documentation:

EXAMPLE GET (to “read” Customer with ID #1)

GET https://myportal.0.site.com/ApiService.svc/Customer/1 HTTP/1.1
Content-Type: application/json 
Token: <enter your unique token here> 
Host: myportal.0.site.com 
Content-Length: 0
Connection: Keep-Alive 
ServerName: myportal

My Logic:

Below is the pom and code.

The pom dependencies:

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.22.1</version>
    </dependency>

The code:

    final String portal = "myportal";
    final String host = portal + ".0.site.com";
    final String baseURL = "https://" + host + "/ApiService.svc";
    final String token = "00000-0000-0000-0000-0000-000000";
    final String getCustomer1URL = baseURL + "/Customer/1";
    final String contentType = "application/json";
    final String contentLength = "0";
    final String connection = "Keep-Alive";

    try {

        // Headers for client.
        ClientRequestFilter clientRequestFilter = new ClientRequestFilter() {
            public void filter(ClientRequestContext clientRequestContext) throws IOException {
                clientRequestContext.getHeaders().add("Token", token);
                clientRequestContext.getHeaders().add("ServerName", serverName);
                clientRequestContext.getHeaders().add("Content-Type", contentType);
                clientRequestContext.getHeaders().add("Content-Length", contentLength);
                clientRequestContext.getHeaders().add("Connection", connection);

            }
        };

        ClientConfig config = new ClientConfig();
        Client client = ClientBuilder.newBuilder().newClient(config);

        //GET Request
        final Response response = client.target(getCustomer1URL).request().get();

        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
        }
        String output = response.readEntity(String.class);
        System.out.println("Output from Server... \n");
        System.out.println(output);
        client.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

What I'm Getting:

With the above, I'm getting a response of <!DOCTYPE html> with html as a webpage including javascript... Which makes me think that perhaps I didn't craft something properly or that I'm not setting the headers properly or something else. Hoping someone can give me a sanity check on what I'm doing wrong.


Solution

  • You haven't registered clientRequestFilter with your ClientConfig. Do something like below -

     ClientConfig config = new ClientConfig();
     config.register(clientRequestFilter);
     Client client = ClientBuilder.newBuilder().newClient(config);
    

    Although, I prefer to create a separate class for clientRequestFilter and register that class -

     config.register(HeadersClientRequestFilter.class);
    

    By this, Jersey will automatically handle creation and destruction of the ClientRequestFilter objects.