尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
完整的数据接口调用过程
整体架构Thymeleaf页面 ↔ SpringBoot代理接口 ↔ 数据网关1. 前端层!DOCTYPE html html langzh-CN xmlns:thhttp://www.thymeleaf.org head meta charsetUTF-8 title公共数据展示/title style body { padding: 30px; font-family: 微软雅黑; } button { padding: 8px 22px; font-size: 15px; cursor: pointer; } table { border-collapse: collapse; width: 90%; margin-top: 20px; } th,td { border:1px solid #ccc; padding:10px; text-align:center; } th { background:#f0f0f0; } .tip { color:#666; margin:10px 0; } .error { color:red; } /style /head body h3淄博公共开放数据查询天气预报/h3 div classtip button idqueryBtn查询7天天气/button /div div idresultBox/div script // WMO天气编码中文映射 const weatherMap { 0: 晴, 3: 多云, 51: 小雨/毛毛雨, 81: 阵雨 }; const queryBtn document.getElementById(queryBtn); const resultBox document.getElementById(resultBox); queryBtn.addEventListener(click, async (){ resultBox.innerHTML p classtip数据加载中.../p; try { // 调用后端代理接口天气接口无分页参数直接去掉page/start const res await fetch(/api/proxy/getW); const rawJson await res.json(); // 网关鉴权失败判断 if(rawJson.code ! 200){ resultBox.innerHTML p classerror接口调用失败${raw.msg}/p; return; } // 解析嵌套JSON字符串 const weatherData JSON.parse(rawJson.data); const daily weatherData.daily; // 拼接页面HTML let html p classtip 经度${weatherData.longitude}纬度${weatherData.latitude}时区${weatherData.timezone} /p table thead tr th日期/th th天气编码/th th天气/th th最高温℃/th th最低温℃/th /tr /thead tbody ; // 遍历日期数组渲染每行 daily.time.forEach((date, index) { const code daily.weathercode[index]; html tr td${date}/td td${code}/td td${weatherMap[code] || 未知天气}/td td${daily.temperature_2m_max[index]}/td td${daily.temperature_2m_min[index]}/td /tr ; }); html /tbody/table; resultBox.innerHTML html; }catch(err){ console.error(err); resultBox.innerHTML p classerror请求异常${err.message}/p; } }) /script /body /html2. SpringBoot后端代理层package demo.controller; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.security.cert.X509Certificate; import java.util.Base64; import java.util.UUID; import javax.net.ssl.*; RestController public class GatewayProxyController { private static final String CLIENT_ID 972ee8d6d************2e521520184; private static final String CLIENT_SECRET f8f4d09*********e395224ebd96; private static final String API_URL https://data.sd.gov.cn/gateway/api/1/get_W; private static final String HMAC_ALGORITHM HmacSHA256; // 静态禁用SSL校验仅本地调试 static { try { TrustManager[] trustAllCerts new TrustManager[]{ new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkClientTrusted(X509Certificate[] certs, String authType) {} public void checkServerTrusted(X509Certificate[] certs, String authType) {} } }; SSLContext sslContext SSLContext.getInstance(TLS); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); HostnameVerifier allHostsValid (hostname, session) - true; HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); } catch (Exception e) { e.printStackTrace(); } } GetMapping(/api/proxy/getW) public String proxyGetW() { try { String timestamp String.valueOf(System.currentTimeMillis()); String nonce UUID.randomUUID().toString(); String textToSign CLIENT_ID timestamp nonce; Mac mac Mac.getInstance(HMAC_ALGORITHM); SecretKeySpec key new SecretKeySpec(CLIENT_SECRET.getBytes(StandardCharsets.UTF_8), HMAC_ALGORITHM); mac.init(key); byte[] hmacData mac.doFinal(textToSign.getBytes(StandardCharsets.UTF_8)); String signature Base64.getEncoder().encodeToString(hmacData); URL url new URL(API_URL); HttpURLConnection conn (HttpURLConnection) url.openConnection(); conn.setRequestMethod(GET); conn.setConnectTimeout(10000); conn.setReadTimeout(10000); conn.setRequestProperty(X-Client-Id, CLIENT_ID); conn.setRequestProperty(X-Timestamp, timestamp); conn.setRequestProperty(X-Nonce, nonce); conn.setRequestProperty(X-Signature, signature); int code conn.getResponseCode(); BufferedReader br new BufferedReader(new InputStreamReader( code 200 code 300 ? conn.getInputStream() : conn.getErrorStream(), StandardCharsets.UTF_8 )); StringBuilder sb new StringBuilder(); String line; while ((line br.readLine()) ! null) { sb.append(line); } br.close(); conn.disconnect(); return sb.toString(); } catch (Exception e) { e.printStackTrace(); return 代理接口异常 e.getMessage(); } } }3.页面跳转控制器Controller public class TestController { GetMapping(/huoqu) public String toHuoquList() { return huoqu-list; } }4.全局跨域配置import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; Configuration public class CorsConfig implements WebMvcConfigurer { Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping(/api/**) .allowedOrigins(*) .allowedMethods(GET) .maxAge(3600); } }
RELATED

相关推荐

Kimi K3:智能的新前沿

Kimi K3:智能的新前沿

Kimi K3:智能的新前沿 原创 Kimi K3 月之暗面 Kimi 2026年7月17日 01:02 天津 犯其至难而图其至远者,发之以勇,守之以专,达之以强。 今天,我们正式推出 Kimi K3,我们迄今能力最强的模型。 Kimi K3 是一个…

📅 2026/11/11 19:21:37
小蜜陪护机器人 - ESP32-CAM 摄像头配置指南

小蜜陪护机器人 - ESP32-CAM 摄像头配置指南

小蜜陪护机器人 ESP32-CAM 摄像头配置指南 一、小蜜陪护机器人使用 ESP32-CAM 摄像头 小蜜陪护机器人集成了计算机视觉能力,能够实时分析家中画面,提供安防监控、老人看护、宠物观察等功能。为了实现这一能力,小蜜主要采用 ESP32-CAM 作为默…

📅 2026/10/8 16:37:18
Apollo Save Tool:PS4游戏存档管理的终极完整指南

Apollo Save Tool:PS4游戏存档管理的终极完整指南

Apollo Save Tool:PS4游戏存档管理的终极完整指南 【免费下载链接】apollo-ps4 Apollo Save Tool (PS4) 项目地址: https://gitcode.com/gh_mirrors/ap/apollo-ps4 你是否曾经因为游戏进度丢失而懊恼?是否想在多台PS4主机间转移存档却束手无策&am…

📅 2026/10/6 6:00:16
MORE NEWS

更多资讯

📰

特种作业操作证工种怎么考?学历不高也能报!官方报名入口在这里

特种作业操作证工种怎么考?学历不高也能报!官方报名入口在这里 你是不是也担心自己学历不高,怕报不了名?别慌,特种作业操作证工种其实对学历要求并不高,只要符合条件,人人都能考。今天我就从 商丘房建工程从业者…

📰

低压电工证价值高吗?千万别踩坑,考前必看避雷指南

低压电工证价值高吗?千万别踩坑,考前必看避雷指南 怕考不过白交培训费?低压电工证价值高吗?这是很多想进入电工行业的朋友最关心的问题。很多人觉得电工证只是走个流程,结果一上手才发现,没有证书根本干不了活,而考不过又浪费时间和钱。别急,本文从…

📰

揭阳市电工证哪里考取 全国通用怎么选才不吃亏

揭阳市电工证哪里考取 全国通用怎么选才不吃亏 你是不是也遇到过这种情况:找电工证培训,跑了好几家机构,价格参差不齐,证书真假难辨?现在全国通用的电工证成了很多人的刚需,但不知道 揭阳市电工证哪里考取…

📰

电工证原题图片真实经历告诉你怎么选靠谱的报考渠道

电工证原题图片真实经历告诉你怎么选靠谱的报考渠道 你是不是也在网上刷到“电工证原题图片真实”的帖子,犹豫要不要考个电工证?报名的时候又担心被中介骗钱?别急,今天我就从一个在焦作转岗的电工从业者的角度,说说我是怎么考下来的,也告诉你哪些地方真…

📰

如何教取电工证通过率揭秘实操考试心里没底怕挂科

如何教取电工证通过率揭秘实操考试心里没底怕挂科 实操考试心里没底怕挂科,这个问题很多人都有,尤其是在郑州金水区这种工程项目密集的地方,电工证几乎是入场的“门槛证”。很多新进的项目现场管理员都问过我:“如何教取电工证?到底能不能一次性通过?”…

📰

电工证图片打印怎么搞?官方报名入口全解析,别白交培训费

电工证图片打印怎么搞?官方报名入口全解析,别白交培训费 怕考不过白交培训费?现在越来越多的电工从业者在考虑报考电工证,尤其是想了解【电工证图片打印】怎么操作,甚至有些人误以为只要打印一张证书图片就能上岗。但你有没有想过,这个证到底能不能打印…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬