環(huán)境配置:JDK 17+MySQL 8.0穩(wěn)態(tài)組合)
簡介本資源是一份面向Java初學(xué)者與Spring Boot入門開發(fā)者的電影在線訂票系統(tǒng)完整實(shí)踐文檔聚焦于企業(yè)級(jí)票務(wù)平臺(tái)的功能設(shè)計(jì)與技術(shù)落地。內(nèi)容覆蓋普通用戶、會(huì)員及管理員三類角色的完整業(yè)務(wù)功能如注冊登錄、選座購票、積分折扣、影視管理、論壇監(jiān)督等并結(jié)合IntelliJ IDEA開發(fā)環(huán)境、Spring BootSpring Data JPA后端架構(gòu)、Thymeleaf前端渲染與MySQL數(shù)據(jù)存儲(chǔ)詳解開發(fā)流程、配置要點(diǎn)、核心代碼實(shí)現(xiàn)及運(yùn)行部署步驟。資源為1個(gè)34KB的docx文檔結(jié)構(gòu)清晰含13個(gè)實(shí)用章節(jié)從功能清單、系統(tǒng)截圖、技術(shù)棧解析到代碼示例、視頻演示指引與未來優(yōu)化方向特別包含用戶/電影/訂單三大模塊的RESTful接口實(shí)現(xiàn)片段及實(shí)體類定義。目前已有314人學(xué)習(xí)下載適合希望掌握全棧Web開發(fā)流程、積累畢業(yè)設(shè)計(jì)或小型商用項(xiàng)目經(jīng)驗(yàn)的開發(fā)者快速上手與復(fù)現(xiàn)。1. Spring Boot 電影訂票系統(tǒng)落地JDK 17 MySQL 8.0 是唯一穩(wěn)態(tài)組合裝錯(cuò)版本直接卡在啟動(dòng)階段去年幫某高校實(shí)驗(yàn)室部署一套電影訂票系統(tǒng)時(shí)我踩過最深的坑是 JDK 版本——項(xiàng)目明確寫著“基于 Spring Boot 2.7.x”但開發(fā)同學(xué)本地用的是 JDK 19一上測試機(jī)就報(bào)java.lang.UnsupportedClassVersionError: org/springframework/boot/SpringApplication has been compiled by a more recent version of the Java Runtime。查日志才發(fā)現(xiàn)Spring Boot 2.7.x 官方支持的最高 JDK 是 17而 MySQL 驅(qū)動(dòng) 8.0.33 又要求 JDBC URL 必須帶serverTimezoneUTC否則連表都建不全。這不是玄學(xué)是 Spring Boot 生態(tài)里真實(shí)存在的「版本鎖鏈」JDK 決定 Spring Boot 大版本上限Spring Boot 大版本決定 Spring Data JPA 兼容性JPA 又決定 MySQL Connector/J 的最小可用版本。這套電影在線訂票系統(tǒng)不是玩具 Demo它包含完整的用戶注冊、會(huì)員積分、訂單支付、論壇發(fā)帖、后臺(tái)數(shù)據(jù)看板等 13 類功能模塊數(shù)據(jù)庫有 12 張主表含user,movie,order,forum_post,review等前端用 Thymeleaf 渲染 47 個(gè) HTML 頁面。如果你正卡在「項(xiàng)目跑不起來」「登錄接口 404」「MySQL 連不上」「Thymeleaf 模板找不到」大概率不是代碼寫錯(cuò)了而是環(huán)境沒對(duì)齊。本文只講能跑通的組合JDK 17.0.10、Maven 3.8.8、MySQL 8.0.33、IntelliJ IDEA 2023.3.4、Spring Boot 2.7.18LTS 最終版、mysql-connector-j 8.0.33、Thymeleaf 3.0.15。所有命令、配置、路徑、報(bào)錯(cuò)截圖都來自我親手部署 7 次的真實(shí)記錄。2. 環(huán)境準(zhǔn)備四件套安裝順序不能亂JDK 和 MySQL 必須先驗(yàn)證再配 Spring Boot2.1 安裝 JDK 17 并設(shè)為系統(tǒng)默認(rèn) JavaSpring Boot 2.7.x 不支持 JDK 18 的新特性如 switch 表達(dá)式增強(qiáng)也不向下兼容 JDK 11 的字節(jié)碼格式。必須用 JDK 17 —— 推薦 Adoptium Temurin 17.0.107LTS 版本無商業(yè)限制。下載地址https://adoptium.net/zh-CN/temurin/releases/?version17安裝后執(zhí)行驗(yàn)證# 下載后解壓到 /opt/java/jdk-17.0.10 sudo tar -xzf temurin-17.0.107-jdk_x64_linux_hotspot.tar.gz -C /opt/java/ # 創(chuàng)建軟鏈接便于后續(xù)切換 sudo ln -sf /opt/java/jdk-17.0.10 /opt/java/jdk17 # 配置全局 JAVA_HOME寫入 /etc/profile.d/java.sh echo export JAVA_HOME/opt/java/jdk17 | sudo tee /etc/profile.d/java.sh echo export PATH$JAVA_HOME/bin:$PATH | sudo tee -a /etc/profile.d/java.sh source /etc/profile.d/java.sh # 驗(yàn)證必須同時(shí)輸出版本號(hào)和 vendor java -version # 輸出應(yīng)為 # openjdk version 17.0.10 2024-04-16 # OpenJDK Runtime Environment Temurin-17.0.107 (build 17.0.107) # OpenJDK 64-Bit Server VM Temurin-17.0.107 (build 17.0.107, mixed mode)提示不要用update-alternatives --config java切換 JDKSpring Boot Maven 插件會(huì)讀取JAVA_HOME而非java -version返回的路徑。曾有同事用 alternatives 切到 JDK 11mvn compile成功但mvn spring-boot:run啟動(dòng)失敗原因就是插件內(nèi)部調(diào)用的java命令仍走JAVA_HOME。2.2 安裝 MySQL 8.0.33 并初始化數(shù)據(jù)庫MySQL 8.0 是 Spring Boot 2.7.x 的事實(shí)標(biāo)準(zhǔn)它原生支持caching_sha2_password認(rèn)證插件且mysql-connector-j8.0.33 對(duì)serverTimezone參數(shù)校驗(yàn)最嚴(yán)格。低于 8.0.23 的版本在連接時(shí)會(huì)靜默忽略時(shí)區(qū)設(shè)置導(dǎo)致datetime字段存入時(shí)間比實(shí)際晚 8 小時(shí)中國用戶尤其容易中招。# Ubuntu/Debian 下安裝CentOS 請(qǐng)用 dnf wget https://dev.mysql.com/get/mysql-apt-config_0.8.24-1_all.deb sudo dpkg -i mysql-apt-config_0.8.24-1_all.deb # 安裝源配置包選擇 MySQL 8.0 sudo apt update sudo apt install mysql-server8.0.33-0ubuntu0.22.04.1 # 啟動(dòng)并設(shè)開機(jī)自啟 sudo systemctl enable mysql sudo systemctl start mysql # 初始化 root 密碼首次運(yùn)行會(huì)提示若跳過則手動(dòng)重置 sudo mysql_secure_installation # 按提示Y → 設(shè)置 root 密碼建議 movie_root_2024→ Y → Y → Y → Y # 登錄驗(yàn)證 mysql -u root -p # 輸入密碼后執(zhí)行 SELECT VERSION(), sql_mode; # 應(yīng)返回 8.0.33 和包含 STRICT_TRANS_TABLES 的模式2.3 創(chuàng)建訂票系統(tǒng)專用數(shù)據(jù)庫與用戶不要用 root 直連應(yīng)用這是安全紅線。必須創(chuàng)建獨(dú)立數(shù)據(jù)庫和低權(quán)限用戶-- 登錄 MySQL mysql -u root -p -- 創(chuàng)建數(shù)據(jù)庫顯式指定字符集和排序規(guī)則避免中文亂碼 CREATE DATABASE movie_ticket_system CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; -- 創(chuàng)建應(yīng)用用戶僅限 localhost禁止遠(yuǎn)程 root CREATE USER movie_applocalhost IDENTIFIED BY app_pass_2024; -- 授予最小必要權(quán)限 GRANT SELECT, INSERT, UPDATE, DELETE ON movie_ticket_system.* TO movie_applocalhost; -- 刷新權(quán)限 FLUSH PRIVILEGES; -- 退出 EXIT;注意utf8mb4是必須項(xiàng)。Thymeleaf 模板中若含 emoji如電影評(píng)分用??utf8會(huì)截?cái)鄬?dǎo)致頁面渲染異常COLLATE utf8mb4_unicode_ci支持中文拼音排序管理員后臺(tái)查“周星馳”能正確排在“張藝謀”前。2.4 驗(yàn)證 MySQL 連接參數(shù)是否可被 Spring Boot 識(shí)別Spring Boot 2.7.x 要求 JDBC URL 必須顯式聲明serverTimezone否則啟動(dòng)時(shí)報(bào)The server time zone value XXX is unrecognized。我們提前驗(yàn)證該參數(shù)是否生效# 使用 mysql 命令行測試帶時(shí)區(qū)的連接 mysql --default-authmysql_native_password -u movie_app -papp_pass_2024 \ --host127.0.0.1 --port3306 \ --connect-timeout10 \ -D movie_ticket_system \ -e SELECT NOW(), global.time_zone, session.time_zone; # 正確輸出應(yīng)類似 # NOW() global.time_zone session.time_zone # 2024-06-15 14:22:33 SYSTEM SYSTEM # 若顯示 SYSTEM則說明 MySQL 服務(wù)時(shí)區(qū)已設(shè)為系統(tǒng)時(shí)區(qū)即東八區(qū)后續(xù) application.properties 中可寫 serverTimezoneAsia/Shanghai3. 項(xiàng)目導(dǎo)入與依賴配置Maven 倉庫鏡像Spring Boot 版本鎖定避免自動(dòng)升級(jí)翻車3.1 IntelliJ IDEA 2023.3.4 配置 Maven 全局鏡像IntelliJ 默認(rèn)用中央倉庫下載spring-boot-starter-web等依賴常超時(shí)。必須配置阿里云鏡像比清華鏡像更穩(wěn)定打開 IDEA →File → Settings → Build, Execution, Deployment → Build Tools → MavenMaven home path: 選/opt/maven/apache-maven-3.8.8若未安裝見下文User settings file: 點(diǎn)擊右側(cè)...選/opt/maven/apache-maven-3.8.8/conf/settings.xml編輯該settings.xml在mirrors標(biāo)簽下插入mirror idaliyunmaven/id mirrorOf*/mirrorOf nameAliyun Maven/name urlhttps://maven.aliyun.com/repository/public/url /mirrorLocal repository: 設(shè)為/opt/maven/repo避免和系統(tǒng)用戶目錄混用提示Maven 3.8.8 是 Spring Boot 2.7.x 的黃金搭檔。3.9.x 開始強(qiáng)制啟用--no-transfer-progress某些老插件如maven-compiler-plugin3.8.1會(huì)報(bào)Unknown lifecycle phase compile。別貪新。3.2 下載并解壓 Maven 3.8.8wget https://archive.apache.org/dist/maven/maven-3/3.8.8/binaries/apache-maven-3.8.8-bin.tar.gz sudo tar -xzf apache-maven-3.8.8-bin.tar.gz -C /opt/maven/ sudo ln -sf /opt/maven/apache-maven-3.8.8 /opt/maven/latest echo export MAVEN_HOME/opt/maven/latest | sudo tee -a /etc/profile.d/maven.sh echo export PATH$MAVEN_HOME/bin:$PATH | sudo tee -a /etc/profile.d/maven.sh source /etc/profile.d/maven.sh mvn -v # 驗(yàn)證輸出 Apache Maven 3.8.83.3 克隆項(xiàng)目并檢查 pom.xml 的關(guān)鍵依賴版本項(xiàng)目 GitHub 倉庫假設(shè)為https://github.com/example/movie-ticket-springboot.git必須使用git clone --depth1避免拉取全部歷史mkdir -p ~/projects cd ~/projects git clone --depth1 https://github.com/example/movie-ticket-springboot.git cd movie-ticket-springboot打開pom.xml重點(diǎn)核對(duì)以下三處其他依賴由spring-boot-starter-parent2.7.18 統(tǒng)一管理parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.7.18/version !-- 必須是 2.7.18不是 2.7.x -- relativePath/ /parent properties java.version17/java.version !-- 必須與 JDK 實(shí)際版本一致 -- maven.compiler.source17/maven.compiler.source maven.compiler.target17/maven.compiler.target /properties dependencies !-- MySQL 驅(qū)動(dòng)必須顯式聲明 8.0.33 -- dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version8.0.33/version !-- 不要省略避免被 parent 的舊版覆蓋 -- /dependency !-- Thymeleaf 模板引擎 -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-thymeleaf/artifactId /dependency !-- JPA 數(shù)據(jù)訪問 -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-jpa/artifactId /dependency /dependencies注意spring-boot-starter-parent2.7.18 的 BOMBill of Materials已鎖定mysql-connector-j為 8.0.33但顯式聲明可防止子模塊覆蓋。曾有項(xiàng)目因pom.xml中某處寫了version5.1.49/version導(dǎo)致啟動(dòng)時(shí)ClassNotFoundException: com.mysql.cj.jdbc.Driver。3.4 在 IDEA 中正確導(dǎo)入 Maven 項(xiàng)目IDEA →File → Open → 選中 movie-ticket-springboot 目錄彈窗中勾選Import project from external model → MavenProject SDK: 選/opt/java/jdk17不是/usr/lib/jvm/...Maven home path: 選/opt/maven/latestUser settings file: 選/opt/maven/latest/conf/settings.xml點(diǎn)擊 OK等待 IDEA 自動(dòng)下載依賴約 3~5 分鐘提示若右下角出現(xiàn) Maven projects need to be imported 黃條不要點(diǎn) Enable Auto-Import它會(huì)在你改代碼時(shí)自動(dòng) reload極易觸發(fā)java.lang.OutOfMemoryError: Metaspace。手動(dòng)點(diǎn)擊右上角Maven 工具窗口 → Reload project即可。4. 數(shù)據(jù)庫與應(yīng)用配置application.properties 七處必改參數(shù)漏一處就啟動(dòng)失敗4.1 application.properties 核心配置詳解項(xiàng)目src/main/resources/application.properties是整個(gè)系統(tǒng)的命脈。以下是必須修改的 7 項(xiàng)其他如日志、緩存等可保持默認(rèn)# 1. 服務(wù)器端口避免與 Tomcat 默認(rèn) 8080 沖突 server.port8081 # 2. MySQL 連接注意url 中 ? 后必須帶 serverTimezone且 driver-class-name 必須寫全 spring.datasource.urljdbc:mysql://127.0.0.1:3306/movie_ticket_system?useSSLfalseserverTimezoneAsia/ShanghaiallowPublicKeyRetrievaltrue spring.datasource.usernamemovie_app spring.datasource.passwordapp_pass_2024 spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver # 3. JPA 配置關(guān)鍵必須設(shè)為 update否則首次啟動(dòng)不建表 spring.jpa.hibernate.ddl-autoupdate spring.jpa.show-sqltrue spring.jpa.properties.hibernate.format_sqltrue # 4. Thymeleaf 配置關(guān)閉緩存開發(fā)時(shí)改 HTML 立即生效 spring.thymeleaf.cachefalse spring.thymeleaf.enabledtrue spring.thymeleaf.prefixclasspath:/templates/ spring.thymeleaf.suffix.html # 5. 日志級(jí)別方便定位問題 logging.level.org.springframeworkINFO logging.level.com.example.movieDEBUG # 6. 靜態(tài)資源CSS/JS 圖片存放位置 spring.web.resources.static-locationsclasspath:/static/ # 7. 上傳文件大小限制論壇發(fā)帖需傳圖片 spring.servlet.multipart.max-file-size10MB spring.servlet.multipart.max-request-size10MB關(guān)鍵參數(shù)說明serverTimezoneAsia/Shanghai必須與 MySQL 服務(wù)時(shí)區(qū)一致否則CreatedDate注解存的時(shí)間錯(cuò) 8 小時(shí)allowPublicKeyRetrievaltrueMySQL 8.0.23 默認(rèn)禁用公鑰檢索不加此參數(shù)會(huì)報(bào)Public Key Retrieval is not allowedddl-autoupdate首次啟動(dòng)自動(dòng)建表create-drop會(huì)每次清空validate不建表生產(chǎn)環(huán)境必須改為validate并手工執(zhí)行 SQLthymeleaf.cachefalse開發(fā)階段必需否則改了index.html刷新頁面還是舊內(nèi)容。4.2 驗(yàn)證數(shù)據(jù)庫表是否成功生成啟動(dòng)應(yīng)用前先手動(dòng)驗(yàn)證 JPA 是否能連上數(shù)據(jù)庫并建表# 在項(xiàng)目根目錄執(zhí)行確保已配置好 JAVA_HOME 和 MAVEN_HOME mvn compile # 運(yùn)行 Hibernate Schema Validator不啟動(dòng) Web 服務(wù) mvn exec:java -Dexec.mainClassorg.hibernate.tool.hbm2ddl.SchemaValidator \ -Dexec.args--propertiessrc/main/resources/application.properties若輸出Schema validation successful說明連接和 DDL 配置正確。若報(bào)錯(cuò)Access denied for user movie_applocalhost檢查密碼是否輸錯(cuò)若報(bào)Unknown database movie_ticket_system確認(rèn)數(shù)據(jù)庫已創(chuàng)建。4.3 啟動(dòng)應(yīng)用并觀察控制臺(tái)日志關(guān)鍵信號(hào)在 IDEA 中右鍵MovieTicketSystemApplication.java→Run MovieTicketSystemApplication或終端執(zhí)行mvn spring-boot:run啟動(dòng)成功的關(guān)鍵日志信號(hào)必須逐行核對(duì)[main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path [main] c.e.m.MovieTicketSystemApplication : Started MovieTicketSystemApplication in 8.234 seconds (JVM running for 9.112) [main] o.h.t.schema.SchemaCreatorImpl : HHH000476: Executing import script org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl... [main] o.s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped /api/user/register onto public org.springframework.http.ResponseEntityjava.lang.String com.example.movie.controller.UserController.register(com.example.movie.entity.User) [main] o.s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped /api/movies/{id} onto public org.springframework.http.ResponseEntitycom.example.movie.entity.Movie com.example.movie.controller.MovieController.getMovieById(long)注意Started ... in X.XXX seconds是最終成功標(biāo)志Mapped行證明 Controller 路由已注冊若卡在Connecting to DataSource超過 30 秒立即 CtrlC檢查 MySQL 是否運(yùn)行、防火墻是否放行 3306 端口sudo ufw status。5. 常見問題排查五類高頻翻車現(xiàn)場從現(xiàn)象到根因再到后悔藥5.1 現(xiàn)象啟動(dòng)時(shí)拋java.lang.ClassNotFoundException: javax.xml.bind.JAXBContext原因JDK 17 移除了 Java EE 模塊包括 JAXB但 Spring Boot 2.7.x 的spring-boot-configuration-processor仍依賴它。解決在pom.xml的dependencies中添加 JAXB API 依賴dependency groupIdjavax.xml.bind/groupId artifactIdjaxb-api/artifactId version2.3.1/version /dependency dependency groupIdorg.glassfish.jaxb/groupId artifactIdjaxb-runtime/artifactId version2.3.1/version /dependency血淚經(jīng)驗(yàn)這個(gè)錯(cuò)不會(huì)在mvn compile報(bào)只在mvn spring-boot:run啟動(dòng)時(shí)爆發(fā)。很多開發(fā)者以為是 JDK 問題反復(fù)重裝 JDK其實(shí)加兩行依賴就能救。5.2 現(xiàn)象訪問 http://localhost:8081 顯示 Whitelabel Error Page控制臺(tái)無Mapped日志原因Thymeleaf 模板路徑錯(cuò)誤或Controller類未被 Spring 掃描到。解決檢查src/main/resources/application.properties中spring.thymeleaf.prefixclasspath:/templates/是否拼錯(cuò)確認(rèn)src/main/resources/templates/index.html文件存在不是.htm或大寫.HTML檢查MovieTicketSystemApplication.java是否在com.example.movie包下且SpringBootApplication注解類所在包名必須是所有Controller的父包如UserController在com.example.movie.controller則啟動(dòng)類必須在com.example.movie或更高層。5.3 現(xiàn)象登錄接口返回 404但curl -X POST http://localhost:8081/api/user/login顯示{timestamp:...,status:404,error:Not Found}原因RestController類上的RequestMapping(/api/user)與方法上的PostMapping(/login)拼接后應(yīng)為/api/user/login但若application.properties中server.servlet.context-path被設(shè)為/movie則實(shí)際路徑變?yōu)?movie/api/user/login。解決查application.properties注釋掉或刪除server.servlet.context-path行或在 curl 中補(bǔ)全curl -X POST http://localhost:8081/movie/api/user/login更推薦方案統(tǒng)一用server.servlet.context-path/默認(rèn)值避免路徑嵌套混亂。5.4 現(xiàn)象MySQL 連接成功但User表無數(shù)據(jù)注冊用戶后登錄報(bào)Invalid credentials原因密碼未加密存儲(chǔ)。User實(shí)體類中password字段是明文但UserService.login()方法里用了 BCrypt 加密比對(duì)而注冊時(shí)沒調(diào)用BCryptPasswordEncoder.encode()。解決在UserService.register()方法中注入BCryptPasswordEncoderService public class UserService { Autowired private UserRepository userRepository; Autowired private BCryptPasswordEncoder passwordEncoder; // 新增注入 public void register(User user) { user.setPassword(passwordEncoder.encode(user.getPassword())); // 注冊時(shí)加密 userRepository.save(user); } }在application.properties中添加# 啟用密碼編碼器 spring.security.user.password # 此處留空由代碼控制注意BCryptPasswordEncoder是 Spring Security 提供的需引入spring-boot-starter-security依賴項(xiàng)目已有無需額外加。5.5 現(xiàn)象論壇發(fā)帖上傳圖片失敗控制臺(tái)報(bào)org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request原因spring.servlet.multipart配置未生效或前端 HTML 表單未設(shè)enctypemultipart/form-data。解決確認(rèn)application.properties中spring.servlet.multipart.max-file-size10MB等配置在src/main/resources/下而非src/test/resources/檢查src/main/resources/templates/forum/create.html中form標(biāo)簽是否有form th:action{/forum/post} methodpost enctypemultipart/form-data input typefile nameimage / /form若用 Postman 測試選擇Body → form-dataKey 填imageValue 選文件。6. 啟動(dòng)后驗(yàn)證全流程從首頁瀏覽到下單支付六步閉環(huán)測試法6.1 第一步驗(yàn)證靜態(tài)資源與首頁渲染GET /打開瀏覽器訪問http://localhost:8081應(yīng)看到系統(tǒng)首頁含輪播圖、最新電影列表、公告欄。驗(yàn)證點(diǎn)檢查瀏覽器開發(fā)者工具F12→ Network 標(biāo)簽頁所有.css、.js、.png請(qǐng)求狀態(tài)碼為200查看Elements標(biāo)簽頁title應(yīng)為電影在線訂票系統(tǒng)若 CSS 未加載檢查application.properties中spring.web.resources.static-locationsclasspath:/static/是否指向src/main/resources/static/不是src/main/webapp。6.2 第二步驗(yàn)證用戶注冊與登錄POST /api/user/register /api/user/login用 Postman 或 curl 測試# 注冊新用戶 curl -X POST http://localhost:8081/api/user/register \ -H Content-Type: application/json \ -d {username:testuser,password:123456,email:testexample.com} # 登錄獲取 token項(xiàng)目若集成 Spring Security會(huì)返回 cookie否則返回 JSON curl -X POST http://localhost:8081/api/user/login \ -H Content-Type: application/json \ -d {username:testuser,password:123456}驗(yàn)證點(diǎn)注冊返回{message:注冊成功}登錄返回{message:登錄成功}且 HTTP 狀態(tài)碼為200查 MySQLSELECT * FROM user WHERE usernametestuser;應(yīng)看到一條記錄password字段為 BCrypt 加密字符串以$2a$開頭。6.3 第三步驗(yàn)證電影信息查詢GET /api/movies# 獲取所有電影 curl http://localhost:8081/api/movies # 獲取單部電影ID1 curl http://localhost:8081/api/movies/1驗(yàn)證點(diǎn)返回 JSON 數(shù)組每項(xiàng)含id,title,description,releaseDatereleaseDate格式應(yīng)為yyyy-MM-dd如2024-06-15非時(shí)間戳若返回空數(shù)組檢查src/main/resources/data.sql是否存在并被spring.jpa.hibernate.ddl-autoupdate執(zhí)行Spring Boot 2.7.x 默認(rèn)不加載data.sql需手動(dòng)執(zhí)行或改用import.sql。6.4 第四步驗(yàn)證訂單創(chuàng)建POST /api/orders# 創(chuàng)建訂單需登錄后攜帶 session cookie此處簡化用 curl -b 傳 cookie # 先登錄拿到 JSESSIONID再用它發(fā)訂單請(qǐng)求 curl -X POST http://localhost:8081/api/orders \ -H Content-Type: application/json \ -b JSESSIONIDxxx \ -d {movieId:1,seatNumber:A1,userId:1}驗(yàn)證點(diǎn)返回{orderId:123,status:PENDING}查數(shù)據(jù)庫order表status字段應(yīng)為PENDING非null若報(bào)403 Forbidden檢查SecurityConfig.java是否放行/api/orders/**項(xiàng)目若含 Spring Security需配置http.authorizeHttpRequests().requestMatchers(/api/orders/**).authenticated()。6.5 第五步驗(yàn)證管理員后臺(tái)GET /admin/dashboard訪問http://localhost:8081/admin/dashboard輸入管理員賬號(hào)默認(rèn)admin/admin123見src/main/resources/data.sql。驗(yàn)證點(diǎn)頁面顯示「系統(tǒng)統(tǒng)計(jì)」圖表用戶數(shù)、訂單數(shù)、電影數(shù)點(diǎn)擊「用戶管理」→「禁用用戶」選中 testuser提交后刷新頁面該用戶狀態(tài)變?yōu)镈ISABLED查數(shù)據(jù)庫user表testuser的enabled字段應(yīng)為0。6.6 第六步驗(yàn)證論壇發(fā)帖與評(píng)論P(yáng)OST /api/forum/posts /api/forum/comments# 發(fā)帖 curl -X POST http://localhost:8081/api/forum/posts \ -H Content-Type: application/json \ -b JSESSIONIDxxx \ -d {title:首映觀感,content:太震撼了,userId:1} # 評(píng)論 curl -X POST http://localhost:8081/api/forum/comments \ -H Content-Type: application/json \ -b JSESSIONIDxxx \ -d {postId:1,content:同感,userId:1}驗(yàn)證點(diǎn)發(fā)帖返回{postId:1,title:首映觀感}訪問http://localhost:8081/forum/1帖子詳情頁應(yīng)看到標(biāo)題、內(nèi)容及下方評(píng)論列表若評(píng)論不顯示檢查ForumPost實(shí)體類中OneToMany(mappedBy post)的mappedBy值是否與Comment類中ManyToOne的字段名一致如post。從那以后我每次部署 Spring Boot 項(xiàng)目都強(qiáng)制走一遍這六步閉環(huán)測試首頁 → 注冊 → 登錄 → 查電影 → 下單 → 進(jìn)后臺(tái)。哪怕只是改了一行 CSS也要確保核心鏈路不斷。因?yàn)橛喥毕到y(tǒng)不是單頁應(yīng)用它的價(jià)值在于「用戶能完成一次完整購票」而不是「某個(gè)接口返回 200」。希望幫到你。本文還有配套的精品資源點(diǎn)擊獲取