【发布时间】:2019-09-09 17:50:24
【问题描述】:
我正在为现有的第三方 REST API 编写一个包装器。第三方的access-token在一小时后过期。所以我不想每次都获取新令牌并决定使用旧令牌,如果它因未经授权的异常而失败,我想获取新的访问令牌然后再次拨打电话。我为它写了下面的代码。
public Store getVendor(String url,boolean tokenreseted) throws Exception {
Store store =null;
try {
store = (Store) RestClient.get(url, headers, queryparam, Store.class);
}
catch (UnauthorizedException e) {
if(!tokenreseted) { //Try with new Access token.
accessToken=getAccessToken();
return getVendor(url,true);
}
else
throw new Exception("UnauthorizedException exception", e);
}
catch (Exception e) {
throw new Exception("Error occured while getting storeIds",e);
}
return store;
}
上面的代码有效。但这是一种好的做法吗?或者还有其他更好的方法吗?
谢谢。
【问题讨论】:
标签: java rest oauth access-token spring-restcontroller