springboot的controller層的常用注解說明
在Spring Boot中,Controller層是用來處理HTTP請求的組件。
下面是Controller層中常用的注解:
1、@RestController
將一個類標識為控制器,并使其支持RESTful風格的API。它是@Controller和@ResponseBody的組合注解。
@Controller 將當前修飾的類注入SpringBoot IOC容器,使得從該類所在的項目跑起來的過程中,這個類就被實例化。
@ResponseBody 它的作用簡短截說就是指該類中所有的API接口返回的數(shù)據(jù),甭管你對應(yīng)的方法返回Map或是其他Object,它會以Json字符串的形式返回給客戶端
@RestController
public class UserController {
// Controller methods
}
2、@RequestMapping
映射HTTP請求到處理方法或控制器類級別。
可以用于類級別的注解來定義基本的URL路徑,并且可以在方法級別的注解中添加進一步的路徑。
@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請求到處理方法。
@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
用于將請求參數(shù)綁定到處理方法的參數(shù)上。
可以指定參數(shù)的名稱、是否必需以及默認值。
@GetMapping("/users")
public List<User> getUsersByRole(@RequestParam("role") String role) {
// Method implementation
}
6、@RequestBody
用于將請求體中的數(shù)據(jù)綁定到處理方法的參數(shù)上,通常用于處理POST請求的JSON數(shù)據(jù)。
@PostMapping("/users")
public User createUser(@RequestBody User user) {
// Method implementation
}
7、@RequestHeader
用于將請求頭中的信息綁定到處理方法的參數(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
用于綁定請求參數(shù)到一個模型對象,并使其可在視圖中訪問。
@GetMapping("/users/{id}")
public String getUserDetails(@PathVariable Long id, @ModelAttribute("message") String message) {
// Method implementation
}
11、@Valid
用于驗證綁定的請求參數(shù),結(jié)合JSR-303 Bean Validation規(guī)范進行數(shù)據(jù)校驗。
@PostMapping("/users")
public ResponseEntity<?> createUser(@Valid @RequestBody User user, BindingResult result) {
if (result.hasErrors()) {
// Handle validation errors
}
// Method implementation
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于SSM框架下各層的解釋說明(Controller等)
這篇文章主要介紹了關(guān)于SSM框架下各層的解釋說明(Controller等),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
使用lombok的@Data會導(dǎo)致棧溢出StackOverflowError問題
這篇文章主要介紹了使用lombok的@Data會導(dǎo)致棧溢出StackOverflowError問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
Java編程之多線程死鎖與線程間通信簡單實現(xiàn)代碼
這篇文章主要介紹了Java編程之多線程死鎖與線程間通信簡單實現(xiàn)代碼,具有一定參考價值,需要的朋友可以了解下。2017-10-10
Android Home鍵監(jiān)聽的實現(xiàn)代碼
這篇文章主要介紹了Android Home 鍵監(jiān)聽的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
在Java的JDBC使用中設(shè)置事務(wù)回滾的保存點的方法
這篇文章主要介紹了在Java的JDBC使用中設(shè)置事務(wù)回滾的保存點的方法,JDBC是Java用于連接各種數(shù)據(jù)庫的API,需要的朋友可以參考下2015-12-12

