当前位置: 首页>后端>正文

SpringBoot常用注解

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閰嶇疆鏂囦欢涓殑<beans>锛屼綔鐢ㄤ负锛氶厤缃畇pring瀹瑰櫒(搴旂敤涓婁笅鏂?銆?/p>

https://blog.csdn.net/javaloveiphone/article/details/52182899

@Bean鏍囨敞鍦ㄦ柟娉曚笂(杩斿洖鏌愪釜瀹炰緥鐨勬柟娉?锛岀瓑浠蜂簬spring鐨剎ml閰嶇疆鏂囦欢涓殑<bean>锛屼綔鐢ㄤ负锛氭敞鍐宐ean瀵硅薄銆?/p>

3. 澶勭悊甯歌鐨?HTTP 璇锋眰绫诲瀷

@RequestMapping

@RequestMapping娉ㄨВ鐨勪富瑕佺敤閫旀槸灏哤eb璇锋眰涓庤姹傚鐞嗙被涓殑鏂规硶杩涜鏄犲皠銆係pring MVC鍜孲pring WebFlux閮介€氳繃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>

鎻愮ず锛氬湪浣跨敤@RequestMapping涔嬪墠锛岃姹傚鐞嗙被杩橀渶瑕佷娇鐢ˊController鎴朄RestController杩涜鏍囪

@RequestMapping鍙互瀵圭被鍜屾柟娉曡繘琛屾爣璁?/strong>锛屽湪瀵圭被杩涜鏍囪锛岃繖鏍风被涓殑澶勭悊鏂规硶鍦ㄦ槧灏勮姹傝矾寰勬椂锛屼細鑷姩灏嗙被涓夽RequestMapping璁剧疆鐨剉alue鎷兼帴鍒版柟娉曚腑鏄犲皠璺緞涔嬪墠锛屽涓嬶細

5 绉嶅父瑙佺殑璇锋眰绫诲瀷:

  • GET 锛氳姹備粠鏈嶅姟鍣ㄨ幏鍙栫壒瀹氳祫婧愩€備妇涓緥瀛愶細GET /users锛堣幏鍙栨墍鏈夊鐢燂級

  • POST 锛氬湪鏈嶅姟鍣ㄤ笂鍒涘缓涓€涓柊鐨勮祫婧愩€備妇涓緥瀛愶細POST /users锛堝垱寤哄鐢燂級

  • PUT 锛氭洿鏂版湇鍔″櫒涓婄殑璧勬簮锛堝鎴风鎻愪緵鏇存柊鍚庣殑鏁翠釜璧勬簮锛夈€備妇涓緥瀛愶細PUT /users/12锛堟洿鏂扮紪鍙蜂负 12 鐨勫鐢燂級

  • DELETE 锛氫粠鏈嶅姟鍣ㄥ垹闄ょ壒瀹氱殑璧勬簮銆備妇涓緥瀛愶細DELETE /users/12锛堝垹闄ょ紪鍙蜂负 12 鐨勫鐢燂級

  • PATCH 锛氭洿鏂版湇鍔″櫒涓婄殑璧勬簮锛堝鎴风鎻愪緵鏇存敼鐨勫睘鎬э紝鍙互鐪嬪仛浣滄槸閮ㄥ垎鏇存柊锛夛紝浣跨敤鐨勬瘮杈冨皯锛岃繖閲屽氨涓嶄妇渚嬪瓙浜嗐€?/p>

3.1. GET 璇锋眰

@GetMapping("users") 绛変环浜?code>@RequestMapping(value="/users",method=RequestMethod.GET)

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n859" mdtype="fences">
@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>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n863" mdtype="fences">@PostMapping("/users")
public ResponseEntity<User> createUser(@Valid @RequestBody UserCreateRequest userCreateRequest) {
return userRespository.save(user);
}</pre>

3.3. PUT 璇锋眰

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n866" mdtype="fences">@PutMapping("/users/{userId}")绛変环浜?/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 璇锋眰

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n868" mdtype="fences">@DeleteMapping("/users/{userId}")绛変环浜?/code>@RequestMapping(value="/users/{userId}",method=RequestMethod.DELETE)
@DeleteMapping("/users/{userId}")
public ResponseEntity deleteUser(@PathVariable(value = "userId") Long userId){
......
}</pre>

3.5. PATCH 璇锋眰

涓€鑸疄闄呴」鐩腑锛屾垜浠兘鏄?PUT 涓嶅鐢ㄤ簡涔嬪悗鎵嶇敤 PATCH 璇锋眰鍘绘洿鏂版暟鎹€?/p>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n871" mdtype="fences"> @PatchMapping("/profile")
public ResponseEntity updateStudent(@RequestBody StudentUpdateRequest studentUpdateRequest) {
studentRepository.updateDetail(studentUpdateRequest);
return ResponseEntity.ok().build();
}</pre>

4. 鍓嶅悗绔紶鍊?/h3>

鎺屾彙鍓嶅悗绔紶鍊肩殑姝g‘濮垮娍锛屾槸浣犲紑濮?CRUD 鐨勭涓€姝ワ紒

4.1. @RequestBody

瀹氫箟: 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")
public ResponseEntity signUp(@RequestBody @Valid UserRegisterRequest userRegisterRequest) {
userService.save(userRegisterRequest);
return ResponseEntity.ok().build();
}</pre>

UserRegisterRequest 瀵硅薄锛?/p>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n881" mdtype="fences">@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserRegisterRequest {
@NotBlank
private String userName;
@NotBlank
private String password;
@FullName
@NotBlank
private String fullName;
}</pre>

鎴戜滑鍙戦€?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 鏍煎紡鐨勬暟鎹槧灏勫埌鎴戜滑鐨?UserRegisterRequest 绫讳笂銆?/p>

[鍥剧墖涓婁紶澶辫触...(image-41b2d5-1675784708278)]

馃憠 闇€瑕佹敞鎰忕殑鏄細涓€涓姹傛柟娉曞彧鍙互鏈変竴涓?code>@RequestBody锛屼絾鏄彲浠ユ湁澶氫釜@RequestParam鍜?code>@PathVariable銆?濡傛灉浣犵殑鏂规硶蹇呴』瑕佺敤涓や釜 @RequestBody鏉ユ帴鍙楁暟鎹殑璇濓紝澶ф鐜囨槸浣犵殑鏁版嵁搴撹璁℃垨鑰呯郴缁熻璁″嚭闂浜嗭紒

4.2. @PathVariable

@PathVariable涓嶡RequestParam閮戒负spring鐨勬敞瑙o紝閮藉彲浠ョ敤浜庡湪Controller灞傛帴鏀跺墠绔紶閫掔殑鏁版嵁锛屼笉杩囦袱鑰呯殑搴旂敤鍦烘櫙涓嶅悓銆?code>@PathVariable鐢ㄤ簬鑾峰彇璺緞鍙傛暟锛?code>@RequestParam鐢ㄤ簬鑾峰彇鏌ヨ鍙傛暟銆傦紙璺緞鍙傛暟鍜?strong>鏌ヨ鍙傛暟锛?/p>

涓句釜绠€鍗曠殑渚嬪瓙锛?/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 )
@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>

SpringBoot常用注解,第1张

4.4.@ResponseBody

5. 璇诲彇閰嶇疆淇℃伅

寰堝鏃跺€欐垜浠渶瑕佸皢涓€浜涘父鐢ㄧ殑閰嶇疆淇℃伅姣斿闃块噷浜?oss銆佸彂閫佺煭淇°€佸井淇¤璇佺殑鐩稿叧閰嶇疆淇℃伅绛夌瓑鏀惧埌閰嶇疆鏂囦欢涓€?/strong>

涓嬮潰鎴戜滑鏉ョ湅涓€涓?Spring 涓烘垜浠彁渚涗簡鍝簺鏂瑰紡甯姪鎴戜滑浠庨厤缃枃浠朵腑璇诲彇杩欎簺閰嶇疆淇℃伅銆?/strong>

鎴戜滑鐨勬暟鎹簮application.yml鍐呭濡備笅锛?/p>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="yaml" cid="n911" mdtype="fences">wuhan2020: 2020骞村垵姝︽眽鐖嗗彂浜嗘柊鍨嬪啝鐘剁梾姣掞紝鐤儏涓ラ噸锛屼絾鏄紝鎴戠浉淇′竴鍒囬兘浼氳繃鍘伙紒姝︽眽鍔犳补锛佷腑鍥藉姞娌癸紒

my-profile:
name: Guide鍝?br> email: koushuangbwcx@163.com

library:
location: 婀栧寳姝︽眽鍔犳补涓浗鍔犳补
books:

  • name: 澶╂墠鍩烘湰娉?br> description: 浜屽崄浜屽瞾鐨勬灄鏈濆鍦ㄧ埗浜茬‘璇婇樋灏旇尐娴烽粯鐥呰繖澶╋紝寰楃煡鑷繁鏆楁亱澶氬勾鐨勬牎鍥敺绁炶4涔嬪嵆灏嗗嚭鍥芥繁閫犵殑娑堟伅鈥斺€斿鏂硅€冨彇鐨勫鏍★紝鎭版槸鐖朵翰褰撳勾涓哄ス鏀惧純鐨勯偅鎵€銆?/li>
  • name: 鏃堕棿鐨勭З搴?br> description: 涓轰粈涔堟垜浠寰楄繃鍘伙紝鑰岄潪鏈潵锛熸椂闂粹€滄祦閫濃€濇剰鍛崇潃浠€涔堬紵鏄垜浠瓨鍦ㄤ簬鏃堕棿涔嬪唴锛岃繕鏄椂闂村瓨鍦ㄤ簬鎴戜滑涔嬩腑锛熷崱娲浡风綏闊﹀埄鐢ㄨ瘲鎰忕殑鏂囧瓧锛岄個璇锋垜浠€濊€冭繖涓€浜樺彜闅鹃鈥斺€旀椂闂寸殑鏈川銆?/li>
  • name: 浜嗕笉璧风殑鎴?br> description: 濡備綍鍏绘垚涓€涓柊涔犳儻锛熷浣曡蹇冩櫤鍙樺緱鏇存垚鐔燂紵濡備綍鎷ユ湁楂樿川閲忕殑鍏崇郴锛?濡備綍璧板嚭浜虹敓鐨勮壈闅炬椂鍒伙紵</pre>

5.1. @value(甯哥敤)

浣跨敤 @Value("${property}") 璇诲彇姣旇緝绠€鍗曠殑閰嶇疆淇℃伅锛?/p>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n914" mdtype="fences">@Value("${wuhan2020}")
String wuhan2020;</pre>

5.2. @ConfigurationProperties(甯哥敤)

閫氳繃@ConfigurationProperties璇诲彇閰嶇疆淇℃伅骞朵笌 bean 缁戝畾銆?/p>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n917" mdtype="fences">@Component
@ConfigurationProperties(prefix = "library")
class LibraryProperties {
@NotEmpty
private String location;
private List<Book> books;

@Setter
@Getter
@ToString
static class Book {
String name;
String description;
}
鐪佺暐getter/setter
......
}</pre>

浣犲彲浠ュ儚浣跨敤鏅€氱殑 Spring bean 涓€鏍凤紝灏嗗叾娉ㄥ叆鍒扮被涓娇鐢ㄣ€?/p>

5.3. PropertySource锛堜笉甯哥敤锛?/h4>

@PropertySource璇诲彇鎸囧畾 properties 鏂囦欢

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n921" mdtype="fences">@Component
@PropertySource("classpath:website.properties")

class WebSite {
@Value("${url}")
private String url;

鐪佺暐getter/setter
......
}</pre>

鏇村鍐呭璇锋煡鐪嬫垜鐨勮繖绡囨枃绔狅細銆?0 鍒嗛挓鎼炲畾 SpringBoot 濡備綍浼橀泤璇诲彇閰嶇疆鏂囦欢锛熴€?銆?/p>

6. 鍙傛暟鏍¢獙

鏁版嵁鐨勬牎楠岀殑閲嶈鎬у氨涓嶇敤璇翠簡锛屽嵆浣垮湪鍓嶇瀵规暟鎹繘琛屾牎楠岀殑鎯呭喌涓嬶紝鎴戜滑杩樻槸瑕佸浼犲叆鍚庣鐨勬暟鎹啀杩涜涓€閬嶆牎楠岋紝閬垮厤鐢ㄦ埛缁曡繃娴忚鍣ㄧ洿鎺ラ€氳繃涓€浜?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常用注解,第2张
img

闈?SpringBoot 椤圭洰闇€瑕佽嚜琛屽紩鍏ョ浉鍏充緷璧栧寘锛岃繖閲屼笉澶氬仛璁茶В锛屽叿浣撳彲浠ユ煡鐪嬫垜鐨勮繖绡囨枃绔狅細銆婂浣曞湪 Spring/Spring Boot 涓仛鍙傛暟鏍¢獙锛熶綘闇€瑕佷簡瑙g殑閮藉湪杩欓噷锛併€嬨€?/p>

馃憠 闇€瑕佹敞鎰忕殑鏄細 鎵€鏈夌殑娉ㄨВ锛屾帹鑽愪娇鐢?JSR 娉ㄨВ锛屽嵆javax.validation.constraints锛岃€屼笉鏄?code>org.hibernate.validator.constraints

6.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)

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n968" mdtype="fences">@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {

@NotNull(message = "classId 涓嶈兘涓虹┖")
private String classId;

@Size(max = 33)
@NotNull(message = "name 涓嶈兘涓虹┖")
private String name;

@Pattern(regexp = "((Man$|^Woman$|UGM$))", message = "sex 鍊间笉鍦ㄥ彲閫夎寖鍥?)
@NotNull(message = "sex 涓嶈兘涓虹┖")
private String sex;

@Email(message = "email 鏍煎紡涓嶆纭?)
@NotNull(message = "email 涓嶈兘涓虹┖")
private String email;

}</pre>

鎴戜滑鍦ㄩ渶瑕侀獙璇佺殑鍙傛暟涓婂姞涓婁簡@Valid娉ㄨВ锛屽鏋滈獙璇佸け璐ワ紝瀹冨皢鎶涘嚭MethodArgumentNotValidException銆?/p>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n970" mdtype="fences">@RestController
@RequestMapping("/api")
public class PersonController {

@PostMapping("/person")
public ResponseEntity<Person> getPerson(@RequestBody @Valid Person person) {
return ResponseEntity.ok().body(person);
}
}</pre>

6.3. 楠岃瘉璇锋眰鍙傛暟(PathVariables 鍜?Request Parameters)

涓€瀹氫竴瀹氫笉瑕佸繕璁板湪绫讳笂鍔犱笂 Validated 娉ㄨВ浜嗭紝杩欎釜鍙傛暟鍙互鍛婅瘔 Spring 鍘绘牎楠屾柟娉曞弬鏁般€?/strong>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n973" mdtype="fences">@RestController
@RequestMapping("/api")
@Validated
public class PersonController {

@GetMapping("/person/{id}")
public ResponseEntity<Integer> getPersonByID(@Valid @PathVariable("id") @Max(value = 5,message = "瓒呰繃 id 鐨勮寖鍥翠簡") Integer id) {
return ResponseEntity.ok().body(id);
}
}</pre>

鏇村鍏充簬濡備綍鍦?Spring 椤圭洰涓繘琛屽弬鏁版牎楠岀殑鍐呭锛岃鐪嬨€婂浣曞湪 Spring/Spring Boot 涓仛鍙傛暟鏍¢獙锛熶綘闇€瑕佷簡瑙g殑閮藉湪杩欓噷锛併€嬭繖绡囨枃绔犮€?/p>

7. 鍏ㄥ眬澶勭悊 Controller 灞傚紓甯?/h3>

浠嬬粛涓€涓嬫垜浠?Spring 椤圭洰蹇呭鐨勫叏灞€澶勭悊 Controller 灞傚紓甯搞€?/p>

鐩稿叧娉ㄨВ锛?/strong>

  1. @ControllerAdvice :娉ㄨВ瀹氫箟鍏ㄥ眬寮傚父澶勭悊绫?/p>

  2. @ExceptionHandler :娉ㄨВ澹版槑寮傚父澶勭悊鏂规硶

濡備綍浣跨敤鍛紵鎷挎垜浠湪绗?5 鑺傚弬鏁版牎楠岃繖鍧楁潵涓句緥瀛愩€傚鏋滄柟娉曞弬鏁颁笉瀵圭殑璇濆氨浼氭姏鍑?code>MethodArgumentNotValidException锛屾垜浠潵澶勭悊杩欎釜寮傚父銆?/p>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n984" mdtype="fences">@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

/**

  • 璇锋眰鍙傛暟寮傚父澶勭悊
    */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request) {
    ......
    }
    }</pre>

鏇村鍏充簬 Spring Boot 寮傚父澶勭悊鐨勫唴瀹癸紝璇风湅鎴戠殑杩欎袱绡囨枃绔狅細

  1. SpringBoot 澶勭悊寮傚父鐨勫嚑绉嶅父瑙佸Э鍔?/p>

  2. 浣跨敤鏋氫妇绠€鍗曞皝瑁呬竴涓紭闆呯殑 Spring Boot 鍏ㄥ眬寮傚父澶勭悊锛?/p>

8. JPA 鐩稿叧

8.1. 鍒涘缓琛?/h4>

@Entity澹版槑涓€涓被瀵瑰簲涓€涓暟鎹簱瀹炰綋銆侤Entity 璇存槑杩欎釜 class 鏄疄浣撶被锛屽苟涓斾娇鐢ㄩ粯璁ょ殑 orm 瑙勫垯锛屽嵆 class 鍚嶅嵆鏁版嵁搴撹〃涓〃鍚嶏紝class 瀛楁鍚嶅嵆琛ㄤ腑鐨勫瓧娈靛悕銆?/p>

@Table 璁剧疆琛ㄦ槑

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n996" mdtype="fences">@Entity
@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. 鍒涘缓涓婚敭


https://www.xamrdz.com/backend/3ze1924307.html

相关文章: