Jersey 2 - Simple Client Get with Headers Example

I am trying to make simple access with headers using Jersey2. I read Jersey 2.x: How to add headers on a RESTful Client to help create my code below, but I'm not very lucky. I created clientRequestFilteras suggested, but I don’t know how to register it with the client correctly, which, I think, can be my main problem.

I hope to get a health check to see what I am doing wrong, need to register clientRequestFilter, or if I am missing more. Below is the documentation of the web service I'm trying to use, and my client code jersey2 to try to use it.

Web Service Documentation:

GET EXAMPLE (for "reading" a client with identifier No. 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 is:

Below is the pom and code.

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 get:

With the above, I get an answer <!DOCTYPE html>with html as a web page, including javascript ... Which makes me think that maybe I didn’t create something properly or that I am not setting the headers or something else correctly. Hoping someone can give me a check on what I'm doing wrong.

+4
source share
1 answer

You have not registered clientRequestFilterwith 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 clientRequestFilterand register this class -

 config.register(HeadersClientRequestFilter.class);

, clientRequestFilter.

+2

Source: https://habr.com/ru/post/1678967/


All Articles