Spring?Boot項目部署及mysql并允許前端本地訪問
1)修改后端配置
確保 <font style="color:rgb(64, 64, 64);">application.yml</font> 或 <font style="color:rgb(64, 64, 64);">application.properties</font> 中的數(shù)據(jù)庫連接指向服務器MySQL:
spring:
datasource:
url: jdbc:mysql://localhost:3306/數(shù)據(jù)庫名?useSSL=false
username: your_user
password: your_password2)解決跨域問題
如果后端未配置 CORS,在 Spring Boot 中添加:
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*") // 允許所有來源(或指定前端地址)
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*");
}
}
2)打包jar
進入文件根目錄(有pom.xml的那一級)
運行
mvn clean package
3)上傳到服務器
4)在服務器運行后端
java -jar C:\Users\Administrator\Desktop\ngpos-boot3-1.0.jar --server.port=8081
檢查是否運行:
ps aux | grep java netstat -tulnp | grep 8080
配置本地Vue前端訪問服務器API
(1)修改Vue的API請求地址
在 <font style="color:rgb(64, 64, 64);">vue.config.js</font> 中配置代理:
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://服務器IP:8080', // 后端地址
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
}
}或者在 <font style="color:rgb(64, 64, 64);">.env.development</font> 中設置:
VUE_APP_API_BASE_URL=http://服務器IP:8080
然后在代碼中使用:
axios.get(`${process.env.VUE_APP_API_BASE_URL}/api/data`);(2)解決跨域問題
如果后端未配置 CORS,在 Spring Boot 中添加:
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*") // 允許所有來源(或指定前端地址)
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*");
}
}測試訪問
(1)直接訪問后端API
在瀏覽器或 Postman 訪問:
http://服務器IP:8080/api/test
如果返回數(shù)據(jù),說明后端正常運行。
(2)本地Vue訪問
運行本地前端:
npm run dev
訪問 <font style="color:rgb(64, 64, 64);">http://localhost:8080</font>,檢查是否能獲取服務器數(shù)據(jù)。
===============================================================
錯誤:
PS C:\Users\17035\Desktop\新建文件夾\back-end-of-western-restaurant - 副本\dish\target> java -jar ngpos-boot3-1.0.jar ngpos-boot3-1.0.jar中沒有主清單屬性
完整的解決方案:
根據(jù)你的 pom.xml 文件,我發(fā)現(xiàn)導致 JAR 文件無法運行的關鍵問題是 spring-boot-maven-plugin 配置中的 true。這個參數(shù)會跳過 Spring Boot 的可執(zhí)行 JAR 打包過程,導致生成的 JAR 文件缺少必要的啟動信息。以下是
- 問題修復步驟
(1) 修改 spring-boot-maven-plugin 配置
將以下部分:
<configuration> <mainClass>com.example.dish.DishApplication</mainClass> <skip>true</skip> <!-- 刪除這一行 --> </configuration>
改為
<configuration>
<mainClass>com.example.dish.DishApplication</mainClass>
</configuration>1. 正確的端口映射命令(管理員權限運行)
netsh interface portproxy add v4tov4 listenport=80 listenaddress=0.0.0.0 connectport=8081 connectaddress=127.0.0.1 protocol=tcp
2. 命令參數(shù)說明
| 參數(shù) | 值 | 說明 |
|---|---|---|
<font style="color:rgb(64, 64, 64);">listenport</font> | 80 | 外部訪問的端口 |
<font style="color:rgb(64, 64, 64);">listenaddress</font> | 0.0.0.0 | 監(jiān)聽所有網(wǎng)絡接口 |
<font style="color:rgb(64, 64, 64);">connectport</font> | 8081 | 內(nèi)部服務實際端口 |
<font style="color:rgb(64, 64, 64);">connectaddress</font> | 127.0.0.1 | 轉發(fā)到本機 |
<font style="color:rgb(64, 64, 64);">protocol</font> | tcp | 使用TCP協(xié)議 |
3. 驗證映射是否成功
netsh interface portproxy show all
預期輸出:
偵聽 ipv4: 連接到 ipv4: 地址 端口 地址 端口 --------------- ---------- --------------- ---------- 0.0.0.0 80 127.0.0.1 8081
4. 開放防火墻80端口(如果尚未開放)
netsh advfirewall firewall add rule name="HTTP_80" dir=in action=allow protocol=TCP localport=80
5. 測試訪問
現(xiàn)在可以通過以下方式訪問:
http://8.138.207.62:80/ticket/listAll
6. 注意事項
- 需要以管理員身份運行CMD/PowerShell
如果80端口被系統(tǒng)服務占用,可改用其他端口(如8080):
netsh interface portproxy add v4tov4 listenport=8080 listenaddress=0.0.0.0 connectport=8081 connectaddress=127.0.0.1
- 重啟后映射會失效,如需持久化可以創(chuàng)建計劃任務
7. 完整操作示例
:: 添加端口映射 netsh interface portproxy add v4tov4 listenport=80 listenaddress=0.0.0.0 connectport=8081 connectaddress=127.0.0.1 :: 查看所有映射規(guī)則 netsh interface portproxy show all :: 開放防火墻 netsh advfirewall firewall add rule name="HTTP_80" dir=in action=allow protocol=TCP localport=80 :: 測試映射是否生效(在服務器本地執(zhí)行) curl http://localhost:80/kitchen/tasks
如果仍然遇到問題,可能是:
- IIS或其他服務占用了80端口(使用
<font style="color:rgb(64, 64, 64);">netstat -ano | findstr :80</font>檢查) - 云服務商限制了80端口(需在控制臺開放)
- 本地網(wǎng)絡屏蔽了80端口(嘗試手機熱點訪問)
總結
到此這篇關于Spring Boot項目部署及mysql并允許前端本地訪問的文章就介紹到這了,更多相關Spring Boot部署mysql前端本地訪問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring使用注解方式實現(xiàn)創(chuàng)建對象
這篇文章主要介紹了Spring使用注解方式實現(xiàn)創(chuàng)建對象,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2023-02-02
IDEA中將SpringBoot項目提交到git倉庫的方法步驟
本文主要介紹了IDEA中將SpringBoot項目提交到git倉庫的方法步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
用Java連接sqlserver數(shù)據(jù)庫時候幾個jar包的區(qū)別分析
這篇文章主要介紹了用Java連接sqlserver數(shù)據(jù)庫時候幾個jar包的區(qū)別分析,需要的朋友可以參考下2014-10-10
Java?C++題解leetcode769最多能完成排序的塊
這篇文章主要為大家介紹了Java?C++題解leetcode769最多能完成排序的塊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
Spring Cloud Config與Bus整合實現(xiàn)微服務配置自動刷新功能
通過整合SpringCloud Config與Spring Cloud Bus,實現(xiàn)了微服務配置的自動刷新功能,這個機制允許一個微服務實例在配置更新時通過消息總線通知其他所有實例同步更新,從而保持配置的一致性并提升系統(tǒng)的運維效率2024-10-10
JetBrains的IDEA編輯器自動配置的JDK版本下載目錄過程
文章說明Java安裝路徑為~/Library/Java/JavaVirtualMachines/ms-17.0.16,并分享配置經(jīng)驗,建議用戶根據(jù)實際需求調(diào)整環(huán)境變量,以確保Java版本正確運行2025-08-08

