您当前的位置:首页 > 百宝箱

Spring Boot 3+JDK 17搭建后端项目实战指南:从零开始的入门教程

2024-11-05 11:14:54 作者:石家庄人才网

鍦ㄧ幇浠婅蒋浠跺紑鍙戦鍩燂紝鏋勫缓楂樻晥銆佸彲缁存姢鐨勭郴缁熸槸鑷冲叧閲嶈鐨勪换鍔°€係pring Boot 3涓嶫DK 17鐨勫畬缇庣粨鍚堬紝涓嶄粎璁╁紑鍙戣繃绋嬫洿鍔犵幇浠e寲鍜屼究鎹凤紝杩樼‘淇濅簡搴旂敤鐨勯珮鎬ц兘銆佸畨鍏ㄦ€у拰鍙墿灞曟€с€傛湰鏂囧皢寮曢浣犱竴姝ユ鎺屾彙濡備綍浣跨敤杩欎袱椤规妧鏈粠闆跺紑濮嬪垱寤轰竴涓疄鎴橀」鐩紝娑电洊浠庣幆澧冨噯澶囧埌娴嬭瘯閮ㄧ讲鐨勫叏杩囩▼銆?/p>

寮曡█

闅忕潃鎶€鏈殑涓嶆柇杩涙锛岃蒋浠跺紑鍙戠殑鏁堢巼鍜屽搧璐ㄨ姹傝秺鏉ヨ秺楂樸€係pring Boot 3涓嶫DK 17鐨勮仈鍚堬紝鐘瑰缁欏紑鍙戣€呯殑涓€鎶婂埄鍣紝璁╄蒋浠跺紑鍙戝彉寰楁洿蹇€佹洿濂姐€佹洿寮恒€係pring Boot 3浣滀负Spring妗嗘灦鐨勬渶鏂扮増鏈紝鑷村姏浜庣畝鍖栧紑鍙戞祦绋嬶紝鎻愪緵寮€绠卞嵆鐢ㄧ殑瑙e喅鏂规锛屽悓鏃朵繚鎸侀珮鎬ц兘鍜屾槗浜庣淮鎶ょ殑鐗规€с€侸DK 17锛屼綔涓篔ava鐨勬渶鏂扮増鏈紝甯︽潵浜嗗椤规€ц兘鏀硅繘銆佹柊鐗规€т互鍙婂鏃х増浠g爜鐨勫吋瀹规€у寮恒€?/p>

鐜鍑嗗

浣犻渶瑕佸畨瑁呭拰閰嶇疆JDK 17銆傚浜庡ぇ澶氭暟Linux鍜孧ac绯荤粺锛屽彲浠ラ€氳繃鍖呯鐞嗗櫒瀹夎JDK 17銆傚湪Linux鐜涓嬶紝浣犲彲浠ヤ娇鐢ㄤ互涓嬪懡浠よ繘琛屽畨瑁咃細

```bash

sudo apt-get update

sudo apt-get install openjdk-17-jdk

```

瀹夎瀹屾垚鍚庯紝浣犲彲浠ラ€氳繃杩愯`java -version`鍛戒护妫€鏌DK鐗堟湰銆?/p>

涓轰簡鏇撮珮鏁堝湴寮€鍙戯紝鎴戜滑鎺ㄨ崘浣跨敤IntelliJ IDEA浣滀负IDE銆傚畠鎻愪緵浜嗗己澶х殑浠g爜鍒嗘瀽銆佽嚜鍔ㄥ畬鎴愬拰璋冭瘯鍔熻兘锛岀壒鍒€傚悎Spring Boot椤圭洰銆?/p>

鏋勫缓鍩虹Spring Boot椤圭洰

浣犲彲浠ュ埄鐢⊿pring Initializr蹇€熷垱寤轰竴涓猄pring Boot椤圭洰銆傚湪绾块€夋嫨鎵€闇€鐨勪緷璧栵紝濡係pring Web銆丼pring Data JPA銆丩ombok绛夛紝骞剁‘淇濇坊鍔燡DK 17缂栬瘧鏀寔銆?/p>

缂栧啓RESTful API

鎺ヤ笅鏉ワ紝浣犲彲浠ュ紑濮嬬紪鍐橲pring Boot API銆傚垱寤轰竴涓猆serController绫伙紝瀹炵幇涓€涓畝鍗曠殑GET鎿嶄綔锛?/p>

```java

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class UserController {

@GetMapping("/users")

public String listUsers() {

return "List of users";

}

}

```

鐒跺悗锛屽垱寤轰竴涓狝pplication鍚姩绫伙紝杩愯main鏂规硶鍚姩搴旂敤锛?/p>

```java

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

```

杩欐牱浣犲氨鍒涘缓浜嗕竴涓畝鍗曠殑RESTful API鎺у埗鍣ㄦ潵瀹炵幇涓€涓熀鏈殑璇诲彇鎿嶄綔銆?/p>

闆嗘垚娴嬭瘯涓庨儴缃?/p>

涓轰簡纭繚浠g爜鐨勫仴澹€э紝鎴戜滑闇€瑕佽繘琛屽崟鍏冩祴璇曘€備娇鐢↗Unit鍜孧ockito杩涜鍗曞厓娴嬭瘯锛屽浣犵殑API杩涜鍏ㄩ潰鐨勬祴璇曘€傛祴璇曞畬鎴愬悗锛屼綘鍙互灏嗗簲鐢ㄦ墦鍖呭苟閮ㄧ讲鍒版湇鍔″櫒銆傞儴缃插畬鎴愬悗锛屼綘鍙互閫氳繃curl鍛戒护鎴栧叾浠朒TTP瀹㈡埛绔祴璇曚綘鐨凙PI銆?/p>

鏈枃灏嗕粠闆跺紑濮嬶紝鍏ㄩ潰鎸囧浣犲浣曞埄鐢⊿pring Boot 3涓嶫DK 17鍒涘缓瀹炴垬椤圭洰锛岃鐩栫幆澧冨噯澶囥€侀」鐩瀯寤恒€丄PI瀹炵幇銆佹祴璇曚笌閮ㄧ讲鐨勫叏杩囩▼銆傜浉淇¢€氳繃鏈枃鐨勫涔狅紝浣犲皢鑳藉鐔熺粌鎺屾彙杩欎袱椤规妧鏈紝涓烘湭鏉ョ殑杞欢寮€鍙戜箣璺墦涓嬪潥瀹炵殑鍩虹銆?鐢熸垚 UserControllerTest 绫荤殑鏂扮瘒绔?/p>

闅忕潃鎶€鏈殑涓嶆柇鍙戝睍锛屾祴璇曞湪杞欢寮€鍙戜腑鐨勯噸瑕佹€ф剤鍙戝嚫鏄俱€備负姝わ紝鎴戜滑鏉ュ垱寤轰竴涓?`UserControllerTest` 绫伙紝纭繚鎴戜滑鐨勭敤鎴风鐞嗗姛鑳借繍琛屾棤璇€?/p>

瀵煎叆鐩稿叧搴?/p>

鎴戜滑闇€瑕佸鍏ヤ竴绯诲垪娴嬭瘯鍙婃ā鎷熷簱锛屼负娴嬭瘯鐜鍋氬ソ鍑嗗銆?/p>

```java

import org.junit.jupiter.api.Test;

import org.junit.jupiter.api.extension.ExtendWith;

import org.mockito.InjectMocks;

import org.mockito.Mock;

import org.mockito.junit.jupiter.MockitoExtension;

import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.;

import static org.mockito.Mockito.;

```

鍒涘缓娴嬭瘯绫?/p>

鎺ヤ笅鏉ワ紝鎴戜滑瀹氫箟 `UserControllerTest` 绫伙紝骞朵负鍏舵敞鍏ユā鎷熺殑鏈嶅姟鍜屾帶鍒跺櫒瀹炰緥銆?/p>

```java

@SpringBootTest

@ExtendWith(MockitoExtension.class)

public class UserControllerTest {

@InjectMocks

private UserController controller;

@Mock

private UserService userService;

}

```

缂栧啓娴嬭瘯鐢ㄤ緥

浠?`testListUsers` 鏂规硶涓轰緥锛屾垜浠ā鎷熺敤鎴锋湇鍔¤繑鍥炵殑鐢ㄦ埛鍒楄〃锛屽苟楠岃瘉鎺у埗鍣ㄨ皟鐢ㄦ湇鍔″悗杩斿洖鐨勭粨鏋滄槸鍚︿笌棰勬湡鐩哥銆?/p>

```java

@Test

public void testListUsers() {

// 妯℃嫙鐢ㄦ埛鏈嶅姟杩斿洖鐨勭敤鎴峰垪琛?/p>

String expected = "List of users";

when(userService.listUsers()).thenReturn(expected);

// 璋冪敤鎺у埗鍣ㄦ柟娉曡幏鍙栫敤鎴峰垪琛?/p>

String actual = controller.listUsers();

// 鏂█瀹為檯杩斿洖缁撴灉涓庨鏈熶竴鑷?/p>

assertEquals(expected, actual);

}

```

閫氳繃杩欐牱鐨勬祴璇曪紝鎴戜滑纭繚浜?`UserController` 涓殑 `listUsers` 鏂规硶鍦ㄨ皟鐢?`UserService` 鏃惰兘澶熸纭幏鍙栫敤鎴峰垪琛ㄣ€?/p>

搴旂敤閮ㄧ讲涔嬫梾 馃殌 馃摝 馃尌锔?馃寪 馃殌 馃摝 馃搧 馃敡 馃挕 馃専 馃敟 鉁?... 浠庢湰鍦板埌浜戠鏃呯▼鐨勮儗鍚庢晠浜嬪拰鎶€宸с€傞儴缃插簲鐢ㄧ殑鑳屽悗鏁呬簨鍜屾妧宸ф槸涓板瘜鐨勩€備粠绠€鍗曠殑鏈湴鏈嶅姟鍣ㄩ儴缃插埌澶嶆潅鐨勪簯鐜閮ㄧ讲锛屾瘡涓€姝ラ兘鍏呮弧浜嗘寫鎴樺拰鏈洪亣銆傛帴涓嬫潵璁╂垜浠繁鍏ュ墫鏋愬湪绾垮浘涔︾鐞嗙郴缁熺殑閮ㄧ讲杩囩▼銆?搴旂敤閮ㄧ讲绛栫暐涓€锛氭湰鍦伴儴缃蹭娇鐢↗AR鎴朩A浠跺惎鍔ㄥ簲鐢ㄩ殢鐫€鎶€鏈殑鍙戝睍锛屽皢搴旂敤鎵撳寘涓篔AR鎴朩A浠跺凡缁忔垚涓轰簡閮ㄧ讲搴旂敤鐨勪竴绉嶅父瑙佹柟寮忋€備綘鍙互閫氳繃Spring Boot鑷甫鐨刴aven鎻掍欢杞绘澗鐢熸垚鍙墽琛岀殑JA浠讹紝鍙渶鍦ㄥ懡浠よ涓緭鍏mvn clean install spring-boot:run`鍗冲彲鍚姩搴旂敤銆?搴旂敤閮ㄧ讲绛栫暐浜岋細浜戦儴缃插浜庝簯閮ㄧ讲鑰岃█锛岄€夋嫨涓€涓彲闈犵殑浜戞湇鍔℃彁渚涘晢锛堝AWS, Google Cloud绛夛級鏄潪甯搁噸瑕佺殑绗竴姝ャ€傚埄鐢ㄤ簯鏈嶅姟鎻愪緵鍟嗘彁渚涚殑瀹瑰櫒鏈嶅姟锛堝Docker锛夛紝浣犲彲浠ヨ交鏉惧湴灏嗗簲鐢ㄦ墦鍖呮垚瀹瑰櫒闀滃儚骞舵帹閫佸埌浜戜粨搴撲腑銆傛帴涓嬫潵鍙渶閰嶇疆鑷姩閮ㄧ讲娴佹按绾匡紙濡侸enkins鎴朑itLab CI锛夛紝鍗冲彲鍦ㄦ瘡娆′唬鐮佸彉鏇存椂鑷姩閮ㄧ讲鏂扮殑搴旂敤鐗堟湰銆?妗堜緥瑙f瀽锛氬湪绾垮浘涔︾鐞嗙郴缁熶互鍦ㄧ嚎鍥句功绠$悊绯荤粺涓轰緥锛屾牳蹇冨姛鑳界殑閮ㄧ讲绛栫暐搴旇鍥寸粫鐫€鍥句功绠$悊銆佺敤鎴风鐞嗗拰鎼滅储鍔熻兘灞曞紑銆傞鍏堜綘闇€瑕佽璁℃暟鎹簱妯″紡涓庡井鏈嶅姟鏋舵瀯妗嗘灦鏉ユ弧瓒宠繖浜涘姛鑳界殑闇€姹傘€傚紑鍙戝畬鎴愬悗锛岀‘淇濇瘡涓姛鑳芥ā鍧楅兘缁忚繃浜嗗崟鍏冩祴璇曞拰闆嗘垚娴嬭瘯浠ヤ繚璇佸簲鐢ㄧ殑绋冲畾鎬с€傛渶鍚庡埄鐢―ocker瀹瑰櫒鍖栧簲鐢ㄥ苟鍒╃敤CI/CD宸ュ叿鑷姩鍖栭儴缃叉祦绋嬨€傞儴缃插畬鎴愬悗锛屼娇鐢ㄧ洃鎺у伐鍏锋寔缁洃鎺у簲鐢ㄦ€ц兘骞跺強鏃惰繘琛屼唬鐮佸鏌ュ拰鎬ц兘浼樺寲銆傞€氳繃杩欎竴绯诲垪姝ラ锛屼綘灏嗕粠闆跺紑濮嬫瀯寤轰竴涓畬鏁寸殑鍚庣椤圭洰骞跺叏闈㈡帉鎻pring Boot涓嶫DK鐨勯泦鎴愬簲鐢ㄦ妧鑳斤紒鏃犺浣犻€夋嫨鍝绛栫暐鏉ラ儴缃蹭綘鐨勫簲鐢ㄩ€夋嫨绗﹀悎鑷繁闇€姹傚拰鐜鐨勬渶鍚堥€傜殑鏂规鏄嚦鍏抽噸瑕佺殑鍚屾椂璁板緱鍏虫敞鎬ц兘鐩戞帶鍜屽畨鍏ㄤ繚闅滀互纭繚浣犵殑搴旂敤濮嬬粓绋冲畾杩愯骞舵彁渚涙渶浣崇敤鎴蜂綋楠岋紒

版权声明:《Spring Boot 3+JDK 17搭建后端项目实战指南:从零开始的入门教程》来自【石家庄人才网】收集整理于网络,不代表本站立场,所有图片文章版权属于原作者,如有侵略,联系删除。
https://www.ymil.cn/baibaoxiang/27282.html