股票详情接口提供重试逻辑
This commit is contained in:
@@ -10,9 +10,7 @@ import io.swagger.annotations.ApiResponses;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@@ -137,10 +135,26 @@ public class MoneyApiController {
|
|||||||
@ResponseBody
|
@ResponseBody
|
||||||
public JSONObject forwardRequest(@RequestParam String stockType, @RequestParam String symbol) {
|
public JSONObject forwardRequest(@RequestParam String stockType, @RequestParam String symbol) {
|
||||||
String url = String.format("https://priceapi.moneycontrol.com/pricefeed/%s/equitycash/%s",stockType,symbol);
|
String url = String.format("https://priceapi.moneycontrol.com/pricefeed/%s/equitycash/%s",stockType,symbol);
|
||||||
|
|
||||||
|
// 设置重试次数
|
||||||
|
int maxRetries = 3;
|
||||||
|
for (int retry = 1; retry <= maxRetries; retry++) {
|
||||||
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
|
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
|
||||||
|
|
||||||
if (responseEntity.getStatusCode().value() == 200 && responseEntity.getBody() != null) {
|
if (responseEntity.getStatusCode().value() == 200 && responseEntity.getBody() != null) {
|
||||||
return JSONObject.parseObject(responseEntity.getBody());
|
return JSONObject.parseObject(responseEntity.getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果不是最后一次重试,则等待一段时间再进行下一次重试
|
||||||
|
if (retry < maxRetries) {
|
||||||
|
try {
|
||||||
|
// 你可以根据需要调整等待的时间
|
||||||
|
Thread.sleep(100); // 1秒钟
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user