//抽取Header的生成公共方法这里
这里一般放第三方对接时需要验证的参数
public Class PushService{
ResponseEntity<String> response = null;
private HttpHeaders getHeader() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set("Authorization", "Basic "+Base64Utils.encodeToString(sentkey.getBytes()));
return headers;
}
/*Post请求提交参数访问接口提交我们提供的数据,是否支持批量推送
依据接口提供方来定,这次对接的我问支持支持批量,他说可以,
给的文档只有单条发送,要个批量发送格式,让我自己琢磨,第二天问怎么发,他让我循环发,碰到这么SB的对接方*/
public void postTest(){
List<PushRecycleData> list = thirddatarpc.getYzRecycleData(user);//sql查询出来的数据封装
if (null != list && list.size() > 0){
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();//表单形式提交请求体
map.add("Key1", list.get(i).getA());
map.add("Key2", list.get(i).getB();
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, getHeader());
response = restTemplate.postForEntity(pushUrl +"/gbd/resident", request, String.class);
}
JSONObject result = JSONObject.parseObject(response.getBody());
}
/**
* get请求获取第三方接口数据存入自己的数据库,供业务需要
*/
public void getTest(){
HttpHeaders header = new HttpHeaders();
header.add("Authorization","Basic "+Base64Utils.encodeToString(sentkey.getBytes()));
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(pushUrl +"/blocks?");
String uri = builder.build().encode().toUri().toString();
response = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET,new HttpEntity<String>(header),String.class);
logger.info("获取信息成功!接口返回信息为:{}",response.getBody().toString());
//将获取的数据通过阿里的fastjson转成对象
List<CommunityData> list =JSONObject.parseObject(response.getBody()).getJSONArray("Data").toJavaList(CommunityData.class);
//数据库入库操作
rpc.insert(list);
}
}
定时器
@Service
public Class Timer{
@Autowired
PushService pushService;
/**
* 获取**定时器
*/
@Scheduled(cron = "${spring.timer.***}")
public void timerA(){
pushService.postTest();
}
}
网友评论