查询今年的国家法定节假日url:http://timor.tech/api/holiday/year
相关代码如下:
/**
* 获取今年国家法定节假日
*/
public static List<JSONObject> getHoliday() throws Exception {
String getHolidayTodayYear_url = "http://timor.tech/api/holiday/year";
//获取国家法定节假日
JSONObject jsonObject = RestTemplateMethod.forward(getHolidayTodayYear_url,null, HttpMethod.GET);
return null;
}
@Autowired
RestTemplate restTemplate;
/**
* 调用第三方服务器接口
* 传入JSONObject表示post请求
* 不传表示get请求
* @param url 路由
* @param jsonObject 参数
* @param method 方法
* @return
*/
public static JSONObject forward(String url,JSONObject jsonObject,HttpMethod method) throws UnsupportedEncodingException {
RestTemplate restTemplate = new RestTemplate();
//此处加编码格式转换
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(org.springframework.http.MediaType.APPLICATION_JSON_UTF8);
httpHeaders.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 httpEntity = null;
if (method == HttpMethod.GET) {
//get请求
httpEntity = new HttpEntity(null, httpHeaders);
} else if (method == HttpMethod.POST){
//post请求
httpEntity = new HttpEntity(jsonObject, httpHeaders);
} else if (method == HttpMethod.DELETE) {
//delete请求
httpEntity = new HttpEntity(jsonObject, httpHeaders);
}
//访问第三方服务器
ResponseEntity<String> exchange = null;
try {
exchange = restTemplate.exchange(url, method, httpEntity, String.class);
} catch (RestClientException e) {
String message = "404 null";
if (message.equals(e.getMessage())) {
return null;
}
String body = new String(((HttpServerErrorException) e).getResponseBodyAsString().getBytes("ISO-8859-1"),"UTF-8");
return JSONObject.parseObject(body,JSONObject.class);
}
return JSONObject.parseObject(exchange.getBody(),JSONObject.class);
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/106552.html