import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
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.RestController;
import java.util.List;
import java.util.stream.Collectors;
/**
* 查询接口
*/
@RestController
@RequestMapping("/data/test")
public class TestController {
@Autowired
private Person personService;
@GetMapping("/list")
public ResponseEntity<Response> getDataList(@RequestParam(defaultValue = "0") Long startIndex, @RequestParam(defaultValue = "200") Integer pageSize,
@RequestParam(required = false) String ptype) throws Exception {
if (StringUtils.isEmpty(startIndex)) {
startIndex = 0l;
}
if (!StringUtils.isEmpty(type)) {
periodType = periodType.toLowerCase();
}
if (pageSize > 200) {
pageSize = 200;
}
List<Person> dataList = personService.getById(startIndex, type, pageSize);
List<Response> codes = dataList.stream().map(t -> {
Response res = new Response();
res.setId(t.getId());
res.setName(t.getClassId());
res.setAge(t.getDay());
return indicator;
}).collect(Collectors.toList());
Response resp = new Response(new IndexBasedPaging(startIndex, codes.size(), pageSize), codes);
return ResponseEntity.ok(resp);
}
}
//获取查询接口的数据
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import com.alibaba.druid.util.StringUtils;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class Task {
@Value("${data.test:/data/test/list}")
private String apiPath;
private int pageSize = 200;
@Autowired
private Person personservice;
@Scheduled(fixedDelayString = "10000")
public void execute() {
Integer batchSize = 1000;
HmacUtils hmacUtil = new HmacUtils(HmacAlgorithms.HMAC_SHA_1, apiSecret);
try {
WebClient webClient = WebClient.builder().baseUrl(url).build();
boolean done = false;
while (!done) {
String accessToken = hmacUtil.hmacHex(String.valueOf(System.currentTimeMillis()).substring(0, 4));
Response response = webClient.get().uri(apiPath + "?startIndex={startIndex}&pageSize={pageSize}&accessToken={accessToken}", index.get() + 1, pageSize, accessToken)
.retrieve().bodyToMono(EconomicIndicatorsResponse.class).block();
List<Indi> chunk = response.getIns();
for (Indi e : chunk) {
if (cache.containsKey(e.getPeriodType())) {
Indi.PeriodType dataPeriodType = dataPeriodTypeEnums.get(e.getPtype());
List<Indi> cacheList = cache.get(e.gettype());
cacheList.add(e);
}
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Response {
private String status;
private String message;
private List<Indi> ins;
}