英文:
Calling API again if it returns Http 401
问题
I'm here to help with the translated code. Here's the translated version:
// 调用以下 API 使用 Jersey
JsonObject response = ConnectionUtil.getwebTarget()
.request()
.header("Authorization", "Bearer " + access_token)
.get(JsonObject.class);
// 这里的 `accessToken` 是一个变量,通过调用另一个 API 获取其值。
// 现在,如果 response.getStatus() 返回 401,我想调用 API 获取令牌,然后再次执行上述命令。
// 我如何防止重复代码?
// 目前,我写了以下代码。
int statusCode = response.getStatus();
if (statusCode == 401) {
accessToken = new AccessToken().getAccessToken();
response = ConnectionUtil.getwebTarget()
.request()
.header("Authorization", "Bearer " + access_token)
.get(JsonObject.class);
statusCode = response.getStatus();
}
// 如何减少两次写 `JsonObject response` 的代码?我至少想在抛出自定义异常之前检查两次 401 状态码。
// 我是 `Java 编程` 新手。有人能告诉我逻辑吗?这是基本编码,但我仍然在努力。
Please note that I've translated the code as you requested. If you have any further questions or need assistance with the logic, feel free to ask!
英文:
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.
答案1
得分: 1
私有的JsonObject doRequest(AccessToken access_token) {
返回 ConnectionUtil.getwebTarget()
.request()
.header("Authorization", "Bearer " + access_token)
.get(JsonObject.class);
}
然后使用它两次:
JsonObject response = doRequest(access_token);
if (response.getStatus() == 401) {
access_token = new AccessToken().getAccessToken();
response = doRequest(access_token);
}
'doRequest' 可能不是最佳选择的名称,但我不知道整体情况是什么 - 即它实际上在做什么?
英文:
Write a subroutine.
private JsonObject doRequest(AccessToken access_token) {
return ConnectionUtil.getwebTarget()
.request()
.header("Authorization", "Bearer "+ access_token)
.get(JsonObject.class);
}
and then use it twice
JsonObject response = doRequest(access_token);
if (response.getStatus() == 401) {
access_token = new AccessToken().getAccessToken();
response = doRequest(access_token);
}
'doRequest' might not be the best name to choose, but I don't know what the big picture is - i.e., what is it actually doing?
答案2
得分: 1
这是一个适合使用do...while
循环的好候选项。do...while
循环保证至少执行一次,是实现这种重试语义的优秀的Java原生工具。
AccessToken accessToken;
JsonObject response;
int retryCount = 0;
final int MAX_RETRIES = 2;
do {
accessToken = new AccessToken().getAccessToken();
response = ConnectionUtil.getwebTarget().request().header("Authorization", "Bearer " + access_token).get(JsonObject.class);
} while (response.getStatus() == 401 && retryCount++ < MAX_RETRIES);
if (response.getStatus() == 401) {
throw new CustomException("");
}
如果你想了解更多,可以查看这个参考链接。
英文:
this is a good candidate for a do...while
loop. do...while
loops guarantee execution at least once, and are a great native java tool to implement this type of retry semantic.
AccessToken accessToken;
JsonObject response;
int retryCount = 0;
final int MAX_RETRIES = 2;
do {
accessToken= new AccessToken().getAccessToken();
response = ConnectionUtil.getwebTarget().request().header("Authorization", "Bearer "+ access_token).get(JsonObject.class);
} while (response.getStatus()==401 && retryCount++ < MAX_RETRIES);
if (response.getStatus() == 401) {
throw new CustomException("");
}
Worth checking out this reference as well if you want to learn more
答案3
得分: 0
如果您正在寻找基于策略的重试方法,可以像这样操作:
RetryPolicy retryPolicy = new RetryPolicy()
.retryIf((ClientResponse response) -> response.getStatus() == 401)
.withDelay(1, TimeUnit.SECONDS)
.withMaxRetries(3);
Failsafe.with(retryPolicy).get(() -> webResource.post(ClientResponse.class, input));
英文:
If you're looking for a policy based retry, you can do something like this.
RetryPolicy retryPolicy = new RetryPolicy()
.retryIf((ClientResponse response) -> response.getStatus() == 401)
.withDelay(1, TimeUnit.SECONDS)
.withMaxRetries(3);
Failsafe.with(retryPolicy).get(() -> webResource.post(ClientResponse.class, input));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论