springboot的controller層的常用注解說(shuō)明
在Spring Boot中,Controller層是用來(lái)處理HTTP請(qǐng)求的組件。
下面是Controller層中常用的注解:
1、@RestController
將一個(gè)類(lèi)標(biāo)識(shí)為控制器,并使其支持RESTful風(fēng)格的API。它是@Controller和@ResponseBody的組合注解。
@Controller 將當(dāng)前修飾的類(lèi)注入SpringBoot IOC容器,使得從該類(lèi)所在的項(xiàng)目跑起來(lái)的過(guò)程中,這個(gè)類(lèi)就被實(shí)例化。
@ResponseBody 它的作用簡(jiǎn)短截說(shuō)就是指該類(lèi)中所有的API接口返回的數(shù)據(jù),甭管你對(duì)應(yīng)的方法返回Map或是其他Object,它會(huì)以Json字符串的形式返回給客戶(hù)端
@RestController public class UserController { // Controller methods }
2、@RequestMapping
映射HTTP請(qǐng)求到處理方法或控制器類(lèi)級(jí)別。
可以用于類(lèi)級(jí)別的注解來(lái)定義基本的URL路徑,并且可以在方法級(jí)別的注解中添加進(jìn)一步的路徑。
@RestController @RequestMapping("/users") public class UserController { // Methods with specific request mappings @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { // Method implementation } @PostMapping public User createUser(@RequestBody User user) { // Method implementation } }
3、@GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping
分別映射HTTP的GET、POST、PUT、DELETE和PATCH請(qǐng)求到處理方法。
@RestController @RequestMapping("/users") public class UserController { @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { // Method implementation } @PostMapping public User createUser(@RequestBody User user) { // Method implementation } @PutMapping("/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { // Method implementation } @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { // Method implementation } @PatchMapping("/{id}") public User partialUpdateUser(@PathVariable Long id, @RequestBody UserPartialUpdateRequest request) { // Method implementation } }
4、@PathVariable
用于將URL路徑中的占位符參數(shù)綁定到處理方法的參數(shù)上
@GetMapping("/users/{id}") public User getUserById(@PathVariable Long id) { // Method implementation }
5、@RequestParam
用于將請(qǐng)求參數(shù)綁定到處理方法的參數(shù)上。
可以指定參數(shù)的名稱(chēng)、是否必需以及默認(rèn)值。
@GetMapping("/users") public List<User> getUsersByRole(@RequestParam("role") String role) { // Method implementation }
6、@RequestBody
用于將請(qǐng)求體中的數(shù)據(jù)綁定到處理方法的參數(shù)上,通常用于處理POST請(qǐng)求的JSON數(shù)據(jù)。
@PostMapping("/users") public User createUser(@RequestBody User user) { // Method implementation }
7、@RequestHeader
用于將請(qǐng)求頭中的信息綁定到處理方法的參數(shù)上。
@GetMapping("/users") public List<User> getUsersByLocale(@RequestHeader("Accept-Language") String locale) { // Method implementation }
8、@ResponseBody
將方法的返回值直接作為HTTP響應(yīng)的內(nèi)容返回,而不是將其解析為視圖。
@GetMapping("/users/{id}") @ResponseBody public User getUserById(@PathVariable Long id) { // Method implementation }
9、@ResponseStatus
設(shè)置響應(yīng)的HTTP狀態(tài)碼。
@DeleteMapping("/users/{id}") @ResponseStatus(HttpStatus.NO_CONTENT) public void deleteUser(@PathVariable Long id) { // Method implementation }
10、@ModelAttribute
用于綁定請(qǐng)求參數(shù)到一個(gè)模型對(duì)象,并使其可在視圖中訪(fǎng)問(wèn)。
@GetMapping("/users/{id}") public String getUserDetails(@PathVariable Long id, @ModelAttribute("message") String message) { // Method implementation }
11、@Valid
用于驗(yàn)證綁定的請(qǐng)求參數(shù),結(jié)合JSR-303 Bean Validation規(guī)范進(jìn)行數(shù)據(jù)校驗(yàn)。
@PostMapping("/users") public ResponseEntity<?> createUser(@Valid @RequestBody User user, BindingResult result) { if (result.hasErrors()) { // Handle validation errors } // Method implementation }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于SSM框架下各層的解釋說(shuō)明(Controller等)
這篇文章主要介紹了關(guān)于SSM框架下各層的解釋說(shuō)明(Controller等),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02使用lombok的@Data會(huì)導(dǎo)致棧溢出StackOverflowError問(wèn)題
這篇文章主要介紹了使用lombok的@Data會(huì)導(dǎo)致棧溢出StackOverflowError問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Java編程之多線(xiàn)程死鎖與線(xiàn)程間通信簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要介紹了Java編程之多線(xiàn)程死鎖與線(xiàn)程間通信簡(jiǎn)單實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10Java NIO Files類(lèi)讀取文件流方式小結(jié)
本文主要介紹了Java NIO Files類(lèi)讀取文件流方式小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Android Home鍵監(jiān)聽(tīng)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android Home 鍵監(jiān)聽(tīng)的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12在Java的JDBC使用中設(shè)置事務(wù)回滾的保存點(diǎn)的方法
這篇文章主要介紹了在Java的JDBC使用中設(shè)置事務(wù)回滾的保存點(diǎn)的方法,JDBC是Java用于連接各種數(shù)據(jù)庫(kù)的API,需要的朋友可以參考下2015-12-12Java利用序列化實(shí)現(xiàn)對(duì)象深度clone的方法
這篇文章主要介紹了Java利用序列化實(shí)現(xiàn)對(duì)象深度clone的方法,實(shí)例分析了java序列化及對(duì)象克隆的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07