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


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")

From another class, I call this client :

try {
    Gson gson = new Gson();         
    StudentAddRequest studentAdd = new StudentAddRequest();
    studentAdd.id = "1234";     
    String addStudentUri = "";     
    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?