https://segmentfault.com/a/1190000022521844
1. @SpringBootApplication
杩欓噷鍏堝崟鐙嫀鍑?code>@SpringBootApplication 娉ㄨВ璇翠竴涓嬶紝铏界劧鎴戜滑涓€鑸笉浼氫富鍔ㄥ幓浣跨敤瀹冦€?/p>
Guide 鍝ワ細杩欎釜娉ㄨВ鏄?Spring Boot 椤圭洰鐨勫熀鐭筹紝鍒涘缓 SpringBoot 椤圭洰涔嬪悗浼氶粯璁ゅ湪涓荤被鍔犱笂銆?/em>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n758" mdtype="fences">@SpringBootApplication
public class SpringSecurityJwtGuideApplication {
public static void main(java.lang.String[] args) {
SpringApplication.run(SpringSecurityJwtGuideApplication.class, args);
}
}</pre>
鎴戜滑鍙互鎶?@SpringBootApplication
鐪嬩綔鏄?@Configuration
銆?code>@EnableAutoConfiguration銆?code>@ComponentScan 娉ㄨВ鐨勯泦鍚堛€?/p>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n760" mdtype="fences">package org.springframework.boot.autoconfigure;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
......
}
package org.springframework.boot;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}</pre>
鏍规嵁 SpringBoot 瀹樼綉锛岃繖涓変釜娉ㄨВ鐨勪綔鐢ㄥ垎鍒槸锛?/p>
@EnableAutoConfiguration
锛氬惎鐢?SpringBoot 鐨勮嚜鍔ㄩ厤缃満鍒?/p>@ComponentScan
锛?鎵弿琚?code>@Component (@Service
,@Controller
)娉ㄨВ鐨?bean锛屾敞瑙i粯璁や細鎵弿璇ョ被鎵€鍦ㄧ殑鍖呬笅鎵€鏈夌殑绫汇€?/p>@Configuration
锛氬厑璁稿湪 Spring 涓婁笅鏂囦腑娉ㄥ唽棰濆鐨?bean 鎴栧鍏ュ叾浠栭厤缃被
2. Spring Bean 鐩稿叧
2.1. @Autowired
鑷姩瀵煎叆瀵硅薄鍒扮被涓紝琚敞鍏ヨ繘鐨勭被鍚屾牱瑕佽 Spring 瀹瑰櫒绠$悊姣斿锛歋ervice 绫绘敞鍏ュ埌 Controller 绫讳腑銆?/p>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n772" mdtype="fences">@Service
public class UserService {
......
}
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
......
}</pre>
https://www.cnblogs.com/fnlingnzb-learner/p/9723834.html
@Autowired 娉ㄨВ锛屽畠鍙互瀵圭被鎴愬憳鍙橀噺銆佹柟娉曞強鏋勯€犲嚱鏁拌繘琛屾爣娉紝瀹屾垚鑷姩瑁呴厤鐨勫伐浣溿€?閫氳繃 @Autowired鐨勪娇鐢ㄦ潵娑堥櫎 set 锛実et鏂规硶銆傚湪浣跨敤@Autowired涔嬪墠锛屾垜浠涓€涓猙ean閰嶇疆璧峰睘鎬ф椂锛屾槸杩欑敤鐢ㄧ殑
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n775" mdtype="fences"><property name="灞炴€у悕" value=" 灞炴€у€?/> </pre>
閫氳繃杩欑鏂瑰紡鏉ワ紝閰嶇疆姣旇緝绻佺悙锛岃€屼笖浠g爜姣旇緝澶氥€傚湪Spring 2.5 寮曞叆浜?@Autowired 娉ㄨВ
閭d箞浣跨敤@Autowired鐨勫師鐞嗘槸浠€涔堬紵
鍏跺疄鍦ㄥ惎鍔╯pring IoC鏃讹紝瀹瑰櫒鑷姩瑁呰浇浜嗕竴涓狝utowiredAnnotationBeanPostProcessor鍚庣疆澶勭悊鍣紝褰撳鍣ㄦ壂鎻忓埌@Autowied銆丂Resource(鏄疌ommonAnnotationBeanPostProcessor鍚庣疆澶勭悊鍣ㄥ鐞嗙殑)鎴朄Inject鏃讹紝灏变細鍦↖oC瀹瑰櫒鑷姩鏌ユ壘闇€瑕佺殑bean锛屽苟瑁呴厤缁欒瀵硅薄鐨勫睘鎬?/p>
娉ㄦ剰浜嬮」锛?/strong>
鍦ㄤ娇鐢ˊAutowired鏃讹紝棣栧厛鍦ㄥ鍣ㄤ腑鏌ヨ瀵瑰簲绫诲瀷鐨刡ean
濡傛灉鏌ヨ缁撴灉鍒氬ソ涓轰竴涓紝灏卞皢璇ean瑁呴厤缁橜Autowired鎸囧畾鐨勬暟鎹?/p>
濡傛灉鏌ヨ鐨勭粨鏋滀笉姝竴涓紝閭d箞@Autowired浼氭牴鎹悕绉版潵鏌ユ壘銆?/p>
濡傛灉鏌ヨ鐨勭粨鏋滀负绌猴紝閭d箞浼氭姏鍑哄紓甯搞€傝В鍐虫柟娉曟椂锛屼娇鐢╮equired=false
2.2. Component
Component
,@Repository
,@Service
, @Controller
鎴戜滑涓€鑸娇鐢?@Autowired
娉ㄨВ璁?Spring 瀹瑰櫒甯垜浠嚜鍔ㄨ閰?bean銆傝鎯虫妸绫绘爣璇嗘垚鍙敤浜?@Autowired
娉ㄨВ鑷姩瑁呴厤鐨?bean 鐨勭被,鍙互閲囩敤浠ヤ笅娉ㄨВ瀹炵幇锛?/p>
@Component
锛氶€氱敤鐨勬敞瑙o紝鍙爣娉ㄤ换鎰忕被涓?Spring
缁勪欢銆傚鏋滀竴涓?Bean 涓嶇煡閬撳睘浜庡摢涓眰锛屽彲浠ヤ娇鐢?code>@Component 娉ㄨВ鏍囨敞銆?/p>@Repository
: 瀵瑰簲鎸佷箙灞傚嵆 Dao 灞傦紝涓昏鐢ㄤ簬鏁版嵁搴撶浉鍏虫搷浣溿€?/p>@Service
: 瀵瑰簲鏈嶅姟灞傦紝涓昏娑夊強涓€浜涘鏉傜殑閫昏緫锛岄渶瑕佺敤鍒?Dao 灞傘€?/p>@Controller
: 瀵瑰簲 Spring MVC 鎺у埗灞傦紝涓昏鐢ㄦ埛鎺ュ彈鐢ㄦ埛璇锋眰骞惰皟鐢?Service 灞傝繑鍥炴暟鎹粰鍓嶇椤甸潰銆?/p>
2.3. @RestController
@RestController
娉ㄨВ鏄?code>@Controller鍜?/code>@ResponseBody
鐨勫悎闆?琛ㄧず杩欐槸涓帶鍒跺櫒 bean,骞朵笖鏄皢鍑芥暟鐨勮繑鍥炲€肩洿 鎺ュ~鍏?HTTP 鍝嶅簲浣撲腑,鏄?REST 椋庢牸鐨勬帶鍒跺櫒銆?/p>
Guide 鍝ワ細鐜板湪閮芥槸鍓嶅悗绔垎绂伙紝璇村疄璇濇垜宸茬粡寰堜箙娌℃湁鐢ㄨ繃@Controller
銆傚鏋滀綘鐨勯」鐩お鑰佷簡鐨勮瘽锛屽氨褰撴垜娌¤銆?/em>
鍗曠嫭浣跨敤 @Controller
涓嶅姞 @ResponseBody
鐨勮瘽涓€鑸娇鐢ㄥ湪瑕佽繑鍥炰竴涓鍥剧殑鎯呭喌锛岃繖绉嶆儏鍐靛睘浜庢瘮杈冧紶缁熺殑 Spring MVC 鐨勫簲鐢紝瀵瑰簲浜庡墠鍚庣涓嶅垎绂荤殑鎯呭喌銆?code>@Controller +@ResponseBody
杩斿洖 JSON 鎴?XML 褰㈠紡鏁版嵁
鍏充簬@RestController
鍜?@Controller
鐨勫姣旓紝璇风湅杩欑瘒鏂囩珷锛欯RestController vs @Controller銆?/p>
鐔欒埅鍚涳細https://www.jianshu.com/p/18ac9f2a325b
鍦⊿pring涓瑻RestController鐨勪綔鐢ㄧ瓑鍚屼簬@Controller + @ResponseBody @Controller娉ㄨВ锛岃〃鏄庝簡杩欎釜绫绘槸涓€涓帶鍒跺櫒绫汇€? @RequestMapping娉ㄨВ鏄敤鏉ユ槧灏勮姹傜殑锛屽嵆鎸囨槑澶勭悊鍣ㄥ彲浠ュ鐞嗗摢浜沀RL璇锋眰锛?strong>璇ユ敞瑙f棦鍙互鐢ㄥ湪绫讳笂锛屼篃鍙互鐢ㄥ湪鏂规硶涓?/strong>銆? 褰撲娇鐢ˊRequestMapping鏍囪鎺у埗鍣ㄧ被鏃讹紝鏂规硶鐨勮姹傚湴鍧€鏄浉瀵圭被鐨勮姹傚湴鍧€鑰岃█鐨勶紱褰撴病鏈変娇鐢ˊRequestMapping鏍囪绫绘椂锛屾柟娉曠殑璇锋眰鍦板潃鏄粷瀵硅矾寰勩€?/p>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="kotlin" cid="n803" mdtype="fences">@Controller
@RequestMapping("/user")
public class UserController{
@RequestMapping("/users")
public String users() {
return "users";
}
}</pre>
姝ゆ椂璇锋眰users鏂规硶鐨剈rl璺緞灏辨槸锛?../user/users銆? 鍙互鐪嬪埌涓婇潰users鏂规硶鐨勮繑鍥炲€兼槸瀛楃涓茬被鍨嬬殑锛岃繖涓氨鏄鐞嗗櫒鍦ㄥ鐞嗗畬浠诲姟鍚庡皢瑕佽烦杞殑椤甸潰銆傚鏋滄兂瑕佹柟娉曠洿鎺ヨ繑鍥炵粨鏋滐紝鑰屼笉鏄烦杞〉闈紝杩欏氨瑕佺敤鍒癅ResponseBody娉ㄨВ浜嗐€?/p>
@ResponseBody琛ㄧず鏂规硶鐨勮繑鍥炲€肩洿鎺ヤ互鎸囧畾鐨勬牸寮忓啓鍏ttp response body涓紝鑰屼笉鏄В鏋愪负璺宠浆璺緞銆傛牸寮忕殑杞崲鏄€氳繃HttpMessageConverter涓殑鏂规硶瀹炵幇鐨勶紝鍥犱负瀹冩槸涓€涓帴鍙o紝鍥犳鐢卞叾瀹炵幇绫诲畬鎴愯浆鎹€?/p>
濡傛灉瑕佹眰鏂规硶杩斿洖鐨勬槸json鏍煎紡鏁版嵁锛岃€屼笉鏄烦杞〉闈紝鍙互鐩存帴鍦ㄧ被涓婃爣娉ˊRestController锛岃€屼笉鐢ㄥ湪姣忎釜鏂规硶涓爣娉ˊResponseBody锛岀畝鍖栦簡寮€鍙戣繃绋嬨€?/p>
2.4. @Scope
澹版槑 Spring Bean 鐨勪綔鐢ㄥ煙锛屼娇鐢ㄦ柟娉?
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n809" mdtype="fences">@Bean
@Scope("singleton")
public Person personSingleton() {
return new Person();
}</pre>
鍥涚甯歌鐨?Spring Bean 鐨勪綔鐢ㄥ煙锛?/strong>
singleton : 鍞竴 bean 瀹炰緥锛孲pring 涓殑 bean 榛樿閮芥槸鍗曚緥鐨勩€?/p>
prototype : 姣忔璇锋眰閮戒細鍒涘缓涓€涓柊鐨?bean 瀹炰緥銆?/p>
request : 姣忎竴娆?HTTP 璇锋眰閮戒細浜х敓涓€涓柊鐨?bean锛岃 bean 浠呭湪褰撳墠 HTTP request 鍐呮湁鏁堛€?/p>
session : 姣忎竴娆?HTTP 璇锋眰閮戒細浜х敓涓€涓柊鐨?bean锛岃 bean 浠呭湪褰撳墠 HTTP session 鍐呮湁鏁堛€?/p>
2.5. Configuration
涓€鑸敤鏉ュ0鏄庨厤缃被锛屽彲浠ヤ娇鐢?@Component
娉ㄨВ鏇夸唬锛屼笉杩囦娇鐢?code>Configuration娉ㄨВ澹版槑閰嶇疆绫绘洿鍔犺涔夊寲銆?/p>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n822" mdtype="fences">@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}</pre>
@Configuration鏍囨敞鍦ㄧ被涓?/strong>锛岀浉褰撲簬鎶婅绫讳綔涓簊pring鐨剎ml閰嶇疆鏂囦欢涓殑 https://blog.csdn.net/javaloveiphone/article/details/52182899 @Bean鏍囨敞鍦ㄦ柟娉曚笂(杩斿洖鏌愪釜瀹炰緥鐨勬柟娉?锛岀瓑浠蜂簬spring鐨剎ml閰嶇疆鏂囦欢涓殑 @RequestMapping @RequestMapping娉ㄨВ鐨勪富瑕佺敤閫旀槸灏哤eb璇锋眰涓庤姹傚鐞嗙被涓殑鏂规硶杩涜鏄犲皠銆係pring MVC鍜孲pring WebFlux閮介€氳繃 鎻愮ず锛氬湪浣跨敤@RequestMapping涔嬪墠锛岃姹傚鐞嗙被杩橀渶瑕佷娇鐢ˊController鎴朄RestController杩涜鏍囪 @RequestMapping鍙互瀵圭被鍜屾柟娉曡繘琛屾爣璁?/strong>锛屽湪瀵圭被杩涜鏍囪锛岃繖鏍风被涓殑澶勭悊鏂规硶鍦ㄦ槧灏勮姹傝矾寰勬椂锛屼細鑷姩灏嗙被涓夽RequestMapping璁剧疆鐨剉alue鎷兼帴鍒版柟娉曚腑鏄犲皠璺緞涔嬪墠锛屽涓嬶細 5 绉嶅父瑙佺殑璇锋眰绫诲瀷: GET 锛氳姹備粠鏈嶅姟鍣ㄨ幏鍙栫壒瀹氳祫婧愩€備妇涓緥瀛愶細 POST 锛氬湪鏈嶅姟鍣ㄤ笂鍒涘缓涓€涓柊鐨勮祫婧愩€備妇涓緥瀛愶細 PUT 锛氭洿鏂版湇鍔″櫒涓婄殑璧勬簮锛堝鎴风鎻愪緵鏇存柊鍚庣殑鏁翠釜璧勬簮锛夈€備妇涓緥瀛愶細 DELETE 锛氫粠鏈嶅姟鍣ㄥ垹闄ょ壒瀹氱殑璧勬簮銆備妇涓緥瀛愶細 PATCH 锛氭洿鏂版湇鍔″櫒涓婄殑璧勬簮锛堝鎴风鎻愪緵鏇存敼鐨勫睘鎬э紝鍙互鐪嬪仛浣滄槸閮ㄥ垎鏇存柊锛夛紝浣跨敤鐨勬瘮杈冨皯锛岃繖閲屽氨涓嶄妇渚嬪瓙浜嗐€?/p> <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n859" mdtype="fences"> 鍏充簬 <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n863" mdtype="fences">@PostMapping("/users") <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n866" mdtype="fences">@PutMapping("/users/{userId}") <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n868" mdtype="fences">@DeleteMapping("/users/{userId}") 涓€鑸疄闄呴」鐩腑锛屾垜浠兘鏄?PUT 涓嶅鐢ㄤ簡涔嬪悗鎵嶇敤 PATCH 璇锋眰鍘绘洿鏂版暟鎹€?/p>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n871" mdtype="fences"> @PatchMapping("/profile") 鎺屾彙鍓嶅悗绔紶鍊肩殑姝g‘濮垮娍锛屾槸浣犲紑濮?CRUD 鐨勭涓€姝ワ紒 瀹氫箟: RequestBody娉ㄨВ鏄疭pringMVC涓嬬殑涓€涓敞瑙?瀹冨彲浠ラ€氳繃HttpMessageConverter鏉ュ皢web璇锋眰锛堝彲鑳芥槸 POST,PUT,DELETE,GET 璇锋眰锛変腑鐨勫弬鏁版槧灏勫埌鏂规硶鐨勫疄浣撳睘鎬т腑,鍚屾椂杩樺彲浠ョ粨鍚圴alid娉ㄨВ鏉ユ牎楠岃姹傚弬鏁版槸鍚︽弧瓒虫潯浠躲€傜敤浜庤鍙?Request 璇锋眰鐨?body 閮ㄥ垎骞朵笖Content-Type 涓?application/json 鏍煎紡鐨勬暟鎹紝绯荤粺浼氫娇鐢?code>HttpMessageConverter鎴栬€呰嚜瀹氫箟鐨?code>HttpMessageConverter灏嗚姹傜殑 body 涓殑 json 瀛楃涓茶浆鎹负 java 瀵硅薄銆傛垜鐢ㄤ竴涓畝鍗曠殑渚嬪瓙鏉ョ粰婕旂ず涓€涓嬪熀鏈娇鐢紒 鎴戜滑鏈変竴涓敞鍐岀殑鎺ュ彛锛?/p>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n879" mdtype="fences">@PostMapping("/sign-up") UserRegisterRequest 瀵硅薄锛?/p>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n881" mdtype="fences">@Data 鎴戜滑鍙戦€?post 璇锋眰鍒拌繖涓帴鍙o紝骞朵笖 body 鎼哄甫 JSON 鏁版嵁锛?/p>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="json" cid="n883" mdtype="fences">{"userName":"coder","fullName":"shuangkou","password":"123456"}</pre> 杩欐牱鎴戜滑鐨勫悗绔氨鍙互鐩存帴鎶?json 鏍煎紡鐨勬暟鎹槧灏勫埌鎴戜滑鐨? [鍥剧墖涓婁紶澶辫触...(image-41b2d5-1675784708278)] 馃憠 闇€瑕佹敞鎰忕殑鏄細涓€涓姹傛柟娉曞彧鍙互鏈変竴涓?code>@RequestBody锛屼絾鏄彲浠ユ湁澶氫釜 @PathVariable涓嶡RequestParam閮戒负spring鐨勬敞瑙o紝閮藉彲浠ョ敤浜庡湪Controller灞傛帴鏀跺墠绔紶閫掔殑鏁版嵁锛屼笉杩囦袱鑰呯殑搴旂敤鍦烘櫙涓嶅悓銆?code>@PathVariable鐢ㄤ簬鑾峰彇璺緞鍙傛暟锛?code>@RequestParam鐢ㄤ簬鑾峰彇鏌ヨ鍙傛暟銆傦紙璺緞鍙傛暟鍜?strong>鏌ヨ鍙傛暟<beans>
锛屼綔鐢ㄤ负锛氶厤缃畇pring瀹瑰櫒(搴旂敤涓婁笅鏂?銆?/p>
<bean>
锛屼綔鐢ㄤ负锛氭敞鍐宐ean瀵硅薄銆?/p>
3. 澶勭悊甯歌鐨?HTTP 璇锋眰绫诲瀷
RquestMappingHandlerMapping
鍜?code>RequestMappingHndlerAdapter涓や釜绫绘潵鎻愪緵瀵笯RequestMapping娉ㄨВ鐨勬敮鎸併€?/p>
@RequestMapping
娉ㄨВ瀵硅姹傚鐞嗙被涓殑璇锋眰澶勭悊鏂规硶杩涜鏍囨敞锛?code>@RequestMapping娉ㄨВ鎷ユ湁浠ヤ笅鐨勫叚涓厤缃睘鎬э細
value
:鏄犲皠鐨勮姹俇RL鎴栬€呭叾鍒悕锛堝父甯镐娇鐢╬ath="/hello"绛夛級method
:鍏煎HTTP鐨勬柟娉曞悕params
:鏍规嵁HTTP鍙傛暟鐨勫瓨鍦ㄣ€佺己鐪佹垨鍊煎璇锋眰杩涜杩囨护header
:鏍规嵁HTTP Header鐨勫瓨鍦ㄣ€佺己鐪佹垨鍊煎璇锋眰杩涜杩囨护consume
:璁惧畾鍦℉TTP璇锋眰姝f枃涓厑璁镐娇鐢ㄧ殑濯掍綋绫诲瀷product
:鍦℉TTP鍝嶅簲浣撲腑鍏佽浣跨敤鐨勫獟浣撶被鍨?/p>
GET /users
锛堣幏鍙栨墍鏈夊鐢燂級POST /users
锛堝垱寤哄鐢燂級PUT /users/12
锛堟洿鏂扮紪鍙蜂负 12 鐨勫鐢燂級DELETE /users/12
锛堝垹闄ょ紪鍙蜂负 12 鐨勫鐢燂級3.1. GET 璇锋眰
@GetMapping("users")
绛変环浜?code>@RequestMapping(value="/users",method=RequestMethod.GET)
@GetMapping("/users")
public ResponseEntity<List<User>> getAllUsers() {
return userRepository.findAll();
}</pre>3.2. POST 璇锋眰
@PostMapping("users")
绛変环浜?code>@RequestMapping(value="/users",method=RequestMethod.POST)@RequestBody
娉ㄨВ鐨勪娇鐢紝鍦ㄤ笅闈㈢殑鈥滃墠鍚庣浼犲€尖€濊繖鍧椾細璁插埌銆?/p>
public ResponseEntity<User> createUser(@Valid @RequestBody UserCreateRequest userCreateRequest) {
return userRespository.save(user);
}</pre>3.3. PUT 璇锋眰
绛変环浜?/code>@RequestMapping(value="/users/{userId}",method=RequestMethod.PUT)
@PutMapping("/users/{userId}")
public ResponseEntity<User> updateUser(@PathVariable(value = "userId") Long userId,
@Valid @RequestBody UserUpdateRequest userUpdateRequest) {
......
}</pre>3.4. DELETE 璇锋眰
绛変环浜?/code>@RequestMapping(value="/users/{userId}",method=RequestMethod.DELETE)
@DeleteMapping("/users/{userId}")
public ResponseEntity deleteUser(@PathVariable(value = "userId") Long userId){
......
}</pre>3.5. PATCH 璇锋眰
public ResponseEntity updateStudent(@RequestBody StudentUpdateRequest studentUpdateRequest) {
studentRepository.updateDetail(studentUpdateRequest);
return ResponseEntity.ok().build();
}</pre>4. 鍓嶅悗绔紶鍊?/h3>
4.1.
@RequestBody
public ResponseEntity signUp(@RequestBody @Valid UserRegisterRequest userRegisterRequest) {
userService.save(userRegisterRequest);
return ResponseEntity.ok().build();
}</pre>
@AllArgsConstructor
@NoArgsConstructor
public class UserRegisterRequest {
@NotBlank
private String userName;
@NotBlank
private String password;
@FullName
@NotBlank
private String fullName;
}</pre>UserRegisterRequest
绫讳笂銆?/p>
@RequestParam
鍜?code>@PathVariable銆?濡傛灉浣犵殑鏂规硶蹇呴』瑕佺敤涓や釜 @RequestBody
鏉ユ帴鍙楁暟鎹殑璇濓紝澶ф鐜囨槸浣犵殑鏁版嵁搴撹璁℃垨鑰呯郴缁熻璁″嚭闂浜嗭紒4.2.
@PathVariable
涓句釜绠€鍗曠殑渚嬪瓙锛?/p>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n891" mdtype="fences">@GetMapping("/klasses/{klassId}/teachers")
public List<Teacher> getKlassRelatedTeachers(
@PathVariable("klassId") Long klassId,
@RequestParam(value = "type", required = false) String type ) {
...
}</pre>
濡傛灉鎴戜滑璇锋眰鐨?url 鏄細/klasses/{123456}/teachers?type=web
閭d箞鎴戜滑鏈嶅姟鑾峰彇鍒扮殑鏁版嵁灏辨槸锛?code>klassId=123456,type=web銆?/p>
1.@PathVariable锛氫粠璺緞閲岄潰鍘昏幏鍙栧彉閲?/strong>
璇锋眰鐨剈rl锛?http://localhost:8080/jfly.openapi/pad/sulfuCheckInfoByMachineNo1/v1/ECCM001
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n896" mdtype="fences">@RequestMapping (value = "/sulfuCheckInfoByMachineNo1/v1/{machineNo}" , method = RequestMethod.GET )
@ResponseBody
public String I_MES_PAD_18( @PathVariable ( "machineNo" ) String machineNo ) {
if ( machineNo != null && ! "" .equals( machineNo )) {
IPadService service = Application.getClassBean (IPadService. class );
List<MesSulfuCheckInfo> infoList = service .findSulfuCheckInfoByMachineNo( machineNo );
if ( infoList != null && ! infoList .isEmpty()) {
jsonString = JSONUtil. getJSONString ( new JSONData( infoList ));
} else {
jsonString = JSONUtil. getErrorJSONString ( "鏈煡璇㈠埌璇ユ満鍙板彿纭寲淇℃伅" );
}
return jsonString ;
} else {
jsonString = JSONUtil. getErrorJSONString ( "鏈哄彴缂栧彿涓嶈兘涓虹┖" );
return jsonString ;
}
}</pre>
4.3.@RequestParam
2.@RequestParam 锛氭槸浠庤姹傞噷闈㈣幏鍙栧弬鏁?/strong> 璇锋眰鐨剈rl锛歨ttp://localhost:8080/jfly.openapi/pad/sulfuCheckInfoByMachineNo/v1?machineNo=ECCM001 鍏朵腑"required = false" 鏄缃姹傚弬鏁颁负闈炲繀椤?/p>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n899" mdtype="fences">@RequestMapping (value = "/sulfuCheckInfoByMachineNo/v1" , method = RequestMethod. GET ) 寰堝鏃跺€欐垜浠渶瑕佸皢涓€浜涘父鐢ㄧ殑閰嶇疆淇℃伅姣斿闃块噷浜?oss銆佸彂閫佺煭淇°€佸井淇¤璇佺殑鐩稿叧閰嶇疆淇℃伅绛夌瓑鏀惧埌閰嶇疆鏂囦欢涓€?/strong> 涓嬮潰鎴戜滑鏉ョ湅涓€涓?Spring 涓烘垜浠彁渚涗簡鍝簺鏂瑰紡甯姪鎴戜滑浠庨厤缃枃浠朵腑璇诲彇杩欎簺閰嶇疆淇℃伅銆?/strong> 鎴戜滑鐨勬暟鎹簮 <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="yaml" cid="n911" mdtype="fences">wuhan2020: 2020骞村垵姝︽眽鐖嗗彂浜嗘柊鍨嬪啝鐘剁梾姣掞紝鐤儏涓ラ噸锛屼絾鏄紝鎴戠浉淇′竴鍒囬兘浼氳繃鍘伙紒姝︽眽鍔犳补锛佷腑鍥藉姞娌癸紒 my-profile: library: 浣跨敤 <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n914" mdtype="fences">@Value("${wuhan2020}") 閫氳繃 <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n917" mdtype="fences">@Component @Setter 浣犲彲浠ュ儚浣跨敤鏅€氱殑 Spring bean 涓€鏍凤紝灏嗗叾娉ㄥ叆鍒扮被涓娇鐢ㄣ€?/p>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n921" mdtype="fences">@Component class WebSite { 鐪佺暐getter/setter 鏇村鍐呭璇锋煡鐪嬫垜鐨勮繖绡囨枃绔狅細銆?0 鍒嗛挓鎼炲畾 SpringBoot 濡備綍浼橀泤璇诲彇閰嶇疆鏂囦欢锛熴€?銆?/p>
鏁版嵁鐨勬牎楠岀殑閲嶈鎬у氨涓嶇敤璇翠簡锛屽嵆浣垮湪鍓嶇瀵规暟鎹繘琛屾牎楠岀殑鎯呭喌涓嬶紝鎴戜滑杩樻槸瑕佸浼犲叆鍚庣鐨勬暟鎹啀杩涜涓€閬嶆牎楠岋紝閬垮厤鐢ㄦ埛缁曡繃娴忚鍣ㄧ洿鎺ラ€氳繃涓€浜?HTTP 宸ュ叿鐩存帴鍚戝悗绔姹備竴浜涜繚娉曟暟鎹€?/strong> JSR(Java Specification Requests锛?/strong> 鏄竴濂?JavaBean 鍙傛暟鏍¢獙鐨勬爣鍑嗭紝瀹冨畾涔変簡寰堝甯哥敤鐨勬牎楠屾敞瑙o紝鎴戜滑鍙互鐩存帴灏嗚繖浜涙敞瑙e姞鍦ㄦ垜浠?JavaBean 鐨勫睘鎬т笂闈紝杩欐牱灏卞彲浠ュ湪闇€瑕佹牎楠岀殑鏃跺€欒繘琛屾牎楠屼簡锛岄潪甯告柟渚匡紒 鏍¢獙鐨勬椂鍊欐垜浠疄闄呯敤鐨勬槸 Hibernate Validator 妗嗘灦銆侶ibernate Validator 鏄?Hibernate 鍥㈤槦鏈€鍒濈殑鏁版嵁鏍¢獙妗嗘灦锛孒ibernate Validator 4.x 鏄?Bean Validation 1.0锛圝SR 303锛夌殑鍙傝€冨疄鐜帮紝Hibernate Validator 5.x 鏄?Bean Validation 1.1锛圝SR 349锛夌殑鍙傝€冨疄鐜帮紝鐩墠鏈€鏂扮増鐨?Hibernate Validator 6.x 鏄?Bean Validation 2.0锛圝SR 380锛夌殑鍙傝€冨疄鐜般€?/p>
SpringBoot 椤圭洰鐨?spring-boot-starter-web 渚濊禆涓凡缁忔湁 hibernate-validator 鍖咃紝涓嶉渶瑕佸紩鐢ㄧ浉鍏充緷璧栥€傚涓嬪浘鎵€绀猴紙閫氳繃 idea 鎻掍欢鈥擬aven Helper 鐢熸垚锛夛細 闈?SpringBoot 椤圭洰闇€瑕佽嚜琛屽紩鍏ョ浉鍏充緷璧栧寘锛岃繖閲屼笉澶氬仛璁茶В锛屽叿浣撳彲浠ユ煡鐪嬫垜鐨勮繖绡囨枃绔狅細銆婂浣曞湪 Spring/Spring Boot 涓仛鍙傛暟鏍¢獙锛熶綘闇€瑕佷簡瑙g殑閮藉湪杩欓噷锛併€嬨€?/p>
馃憠 闇€瑕佹敞鎰忕殑鏄細 鎵€鏈夌殑娉ㄨВ锛屾帹鑽愪娇鐢?JSR 娉ㄨВ锛屽嵆 ...... <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n968" mdtype="fences">@Data @NotNull(message = "classId 涓嶈兘涓虹┖") @Size(max = 33) @Pattern(regexp = "((Man$|^Woman$|UGM$))", message = "sex 鍊间笉鍦ㄥ彲閫夎寖鍥?) @Email(message = "email 鏍煎紡涓嶆纭?) }</pre> 鎴戜滑鍦ㄩ渶瑕侀獙璇佺殑鍙傛暟涓婂姞涓婁簡 <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n970" mdtype="fences">@RestController @PostMapping("/person") 涓€瀹氫竴瀹氫笉瑕佸繕璁板湪绫讳笂鍔犱笂 <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n973" mdtype="fences">@RestController @GetMapping("/person/{id}") 鏇村鍏充簬濡備綍鍦?Spring 椤圭洰涓繘琛屽弬鏁版牎楠岀殑鍐呭锛岃鐪嬨€婂浣曞湪 Spring/Spring Boot 涓仛鍙傛暟鏍¢獙锛熶綘闇€瑕佷簡瑙g殑閮藉湪杩欓噷锛併€嬭繖绡囨枃绔犮€?/p>
浠嬬粛涓€涓嬫垜浠?Spring 椤圭洰蹇呭鐨勫叏灞€澶勭悊 Controller 灞傚紓甯搞€?/p>
鐩稿叧娉ㄨВ锛?/strong> 濡備綍浣跨敤鍛紵鎷挎垜浠湪绗?5 鑺傚弬鏁版牎楠岃繖鍧楁潵涓句緥瀛愩€傚鏋滄柟娉曞弬鏁颁笉瀵圭殑璇濆氨浼氭姏鍑?code>MethodArgumentNotValidException锛屾垜浠潵澶勭悊杩欎釜寮傚父銆?/p>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n984" mdtype="fences">@ControllerAdvice /** 鏇村鍏充簬 Spring Boot 寮傚父澶勭悊鐨勫唴瀹癸紝璇风湅鎴戠殑杩欎袱绡囨枃绔狅細 SpringBoot 澶勭悊寮傚父鐨勫嚑绉嶅父瑙佸Э鍔?/p> 浣跨敤鏋氫妇绠€鍗曞皝瑁呬竴涓紭闆呯殑 Spring Boot 鍏ㄥ眬寮傚父澶勭悊锛?/p> <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n996" mdtype="fences">@Entity
@ResponseBody
public String I_MES_PAD_16( @RequestParam (value = "machineNo" , required = false ) String machineNo ) {
if ( machineNo != null && ! "" .equals( machineNo )) {
IPadService service = Application. getClassBean (IPadService. class );
List<MesSulfuCheckInfo> infoList = service .findSulfuCheckInfoByMachineNo( machineNo );
if ( infoList != null && ! infoList .isEmpty()) {
jsonString = JSONUtil. getJSONString ( new JSONData( infoList ));
} else {
jsonString = JSONUtil. getErrorJSONString ( "鏈煡璇㈠埌璇ユ満鍙板彿纭寲淇℃伅" );
}
return jsonString ;
} else {
jsonString = JSONUtil. getErrorJSONString ( "鏈哄彴缂栧彿涓嶈兘涓虹┖" );
return jsonString ;
}
}</pre>4.4.
@ResponseBody
5. 璇诲彇閰嶇疆淇℃伅
application.yml
鍐呭濡備笅锛?/p>
name: Guide鍝?br>
email: koushuangbwcx@163.com
location: 婀栧寳姝︽眽鍔犳补涓浗鍔犳补
books:
5.1.
@value
(甯哥敤)@Value("${property}")
璇诲彇姣旇緝绠€鍗曠殑閰嶇疆淇℃伅锛?/p>
String wuhan2020;</pre>5.2.
@ConfigurationProperties
(甯哥敤)@ConfigurationProperties
璇诲彇閰嶇疆淇℃伅骞朵笌 bean 缁戝畾銆?/p>
@ConfigurationProperties(prefix = "library")
class LibraryProperties {
@NotEmpty
private String location;
private List<Book> books;
@Getter
@ToString
static class Book {
String name;
String description;
}
鐪佺暐getter/setter
......
}</pre>5.3.
PropertySource
锛堜笉甯哥敤锛?/h4>
@PropertySource
璇诲彇鎸囧畾 properties 鏂囦欢
@PropertySource("classpath:website.properties")
@Value("${url}")
private String url;
......
}</pre>6. 鍙傛暟鏍¢獙
javax.validation.constraints
锛岃€屼笉鏄?code>org.hibernate.validator.constraints6.1. 涓€浜涘父鐢ㄧ殑瀛楁楠岃瘉鐨勬敞瑙?/h4>
@NotEmpty
琚敞閲婄殑瀛楃涓茬殑涓嶈兘涓?null 涔熶笉鑳戒负绌?/p>@NotBlank
琚敞閲婄殑瀛楃涓查潪 null锛屽苟涓斿繀椤诲寘鍚竴涓潪绌虹櫧瀛楃@Null
琚敞閲婄殑鍏冪礌蹇呴』涓?null@NotNull
琚敞閲婄殑鍏冪礌蹇呴』涓嶄负 null@AssertTrue
琚敞閲婄殑鍏冪礌蹇呴』涓?true@AssertFalse
琚敞閲婄殑鍏冪礌蹇呴』涓?false@Pattern(regex=,flag=)
琚敞閲婄殑鍏冪礌蹇呴』绗﹀悎鎸囧畾鐨勬鍒欒〃杈惧紡@Email
琚敞閲婄殑鍏冪礌蹇呴』鏄?Email 鏍煎紡銆?/p>@Min(value)
琚敞閲婄殑鍏冪礌蹇呴』鏄竴涓暟瀛楋紝鍏跺€煎繀椤诲ぇ浜庣瓑浜庢寚瀹氱殑鏈€灏忓€?/p>@Max(value)
琚敞閲婄殑鍏冪礌蹇呴』鏄竴涓暟瀛楋紝鍏跺€煎繀椤诲皬浜庣瓑浜庢寚瀹氱殑鏈€澶у€?/p>@DecimalMin(value)
琚敞閲婄殑鍏冪礌蹇呴』鏄竴涓暟瀛楋紝鍏跺€煎繀椤诲ぇ浜庣瓑浜庢寚瀹氱殑鏈€灏忓€?/p>@DecimalMax(value)
琚敞閲婄殑鍏冪礌蹇呴』鏄竴涓暟瀛楋紝鍏跺€煎繀椤诲皬浜庣瓑浜庢寚瀹氱殑鏈€澶у€?/p>@Size(max=, min=)
琚敞閲婄殑鍏冪礌鐨勫ぇ灏忓繀椤诲湪鎸囧畾鐨勮寖鍥村唴@Digits (integer, fraction)
琚敞閲婄殑鍏冪礌蹇呴』鏄竴涓暟瀛楋紝鍏跺€煎繀椤诲湪鍙帴鍙楃殑鑼冨洿鍐?/p>@Past
琚敞閲婄殑鍏冪礌蹇呴』鏄竴涓繃鍘荤殑鏃ユ湡@Future
琚敞閲婄殑鍏冪礌蹇呴』鏄竴涓皢鏉ョ殑鏃ユ湡6.2. 楠岃瘉璇锋眰浣?RequestBody)
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String classId;
@NotNull(message = "name 涓嶈兘涓虹┖")
private String name;
@NotNull(message = "sex 涓嶈兘涓虹┖")
private String sex;
@NotNull(message = "email 涓嶈兘涓虹┖")
private String email;@Valid
娉ㄨВ锛屽鏋滈獙璇佸け璐ワ紝瀹冨皢鎶涘嚭MethodArgumentNotValidException
銆?/p>
@RequestMapping("/api")
public class PersonController {
public ResponseEntity<Person> getPerson(@RequestBody @Valid Person person) {
return ResponseEntity.ok().body(person);
}
}</pre>6.3. 楠岃瘉璇锋眰鍙傛暟(PathVariables 鍜?Request Parameters)
Validated
娉ㄨВ浜嗭紝杩欎釜鍙傛暟鍙互鍛婅瘔 Spring 鍘绘牎楠屾柟娉曞弬鏁般€?/strong>
@RequestMapping("/api")
@Validated
public class PersonController {
public ResponseEntity<Integer> getPersonByID(@Valid @PathVariable("id") @Max(value = 5,message = "瓒呰繃 id 鐨勮寖鍥翠簡") Integer id) {
return ResponseEntity.ok().body(id);
}
}</pre>7. 鍏ㄥ眬澶勭悊 Controller 灞傚紓甯?/h3>
@ControllerAdvice
:娉ㄨВ瀹氫箟鍏ㄥ眬寮傚父澶勭悊绫?/p>@ExceptionHandler
:娉ㄨВ澹版槑寮傚父澶勭悊鏂规硶
@ResponseBody
public class GlobalExceptionHandler {
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request) {
......
}
}</pre>
8. JPA 鐩稿叧
8.1. 鍒涘缓琛?/h4>
@Entity
澹版槑涓€涓被瀵瑰簲涓€涓暟鎹簱瀹炰綋銆侤Entity 璇存槑杩欎釜 class 鏄疄浣撶被锛屽苟涓斾娇鐢ㄩ粯璁ょ殑 orm 瑙勫垯锛屽嵆 class 鍚嶅嵆鏁版嵁搴撹〃涓〃鍚嶏紝class 瀛楁鍚嶅嵆琛ㄤ腑鐨勫瓧娈靛悕銆?/p>
@Table
璁剧疆琛ㄦ槑
@Table(name = "role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
鐪佺暐getter/setter......
}</pre>8.2. 鍒涘缓涓婚敭