I am calling below API using jersey
JsonObject response = ConnectionUtil.getwebTarget()
.request().
header("Authorization", "Bearer "+ access_token)
.get(JsonObject.class);
Here accessToken
is variable which got value by calling another API. Now if response.getStatus() returnd 401 I want to call the API to get token and will call the above commnad again. how can I stop myself from duplication?
As of now I am writing below code.
JsonObject response = ConnectionUtil.getwebTarget()
.request().
header("Authorization", "Bearer "+ access_token)
.get(JsonObject.class);
if(response.getStatus()==401)
{
accessToken= new AccessToken().getAccessToken();
JsonObject response = ConnectionUtil.getwebTarget()
.request()
.header("Authorization", "Bearer "+ access_token)
.get(JsonObject.class);
}
how can I reduce writing the code of line JsonObject response
twice. I atleast want to check twice 401 code before throwing customized exception
I am new to java programming
. can somebody tell me logic. its basic coding but I am still struggling.