WebClient 获取接口信息


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;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值