Home » Java » Not able to make REST Calls with Jersey based client

Not able to make REST Calls with Jersey based client

Posted by: admin August 10, 2018 Leave a comment

Questions:

I’ve a Jersey based REST Client and a POST call defined as this :

public ClientResponse post(URI uri,String body)
{
    return client.resource(uri)
            .header("Authorization", "user" + ":" + "password")
            .type(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON)
            .post(ClientResponse.class,body);
}

From another class, I call this client :

try {
    Gson gson = new Gson();         
    StudentAddRequest studentAdd = new StudentAddRequest();
    studentAdd.id = "1234";     
    String addStudentUri = "https://10.254.96.39:8000/students/";     
    String json = gson.toJson(studentAdd);
    ClientResponse response = client.post(URI.create(addStudentUri), json);
    String s = response.getEntity(String.class);
    } catch (Exception e) {
    _log.info("Exception...." + e.getMessage());            
    }

The API endpoint takes only the id in JSON format in the body :

{
   id : 1234
}

The request header is defined as :

Content-Type: application/json
Authorization: <username>:<password>

It works when I submit the request with a browser based REST Client such as POSTMAN but my Java Code is not able to invoke this API.

I’m not getting any kind of error logs in my application, and even the exception path isn’t hit. The code doesn’t come out after the client.post() call.

Can you please help?

Answers: