今天在工作中遇到了一个需求: 查询本年所有的节假日返回给前端展示。
在网上找到了一个可以免费查询当年节假日的接口:
非常感谢了
最终写出来的代码也分享给大家了
@GetMapping("/getDatePlan")
public ResultData test(){
//最终结果
List<Map> result = new ArrayList();
//查询当前年
int year = LocalDateTime.now().getYear();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
HttpEntity<String> entity = new HttpEntity<>( headers );
Map holidayData = (Map) restTemplate.exchange("http://timor.tech/api/holiday/year/"+year, HttpMethod.GET, entity, Map.class).getBody().get("holiday");
holidayData.forEach( (key,value) ->{
Map info = (Map) value;
boolean isHoliday = Boolean.valueOf( info.get("holiday").toString() );
if(isHoliday){
//只保存假期数据
Map<String,String> data = new HashMap<>(8);
//时间
data.put("date",key.toString());
//假期描述
data.put("desc",info.get("name").toString());
result.add(data);
}
});
return ResultData.ok( result );
}