springboot的controller層的常用注解說明
在Spring Boot中,Controller層是用來處理HTTP請求的組件。
下面是Controller層中常用的注解:
1、@RestController
將一個類標(biāo)識為控制器,并使其支持RESTful風(fēng)格的API。它是@Controller和@ResponseBody的組合注解。
@Controller 將當(dāng)前修飾的類注入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ù)的名稱、是否必需以及默認(rèn)值。
@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)文章
SpringBoot結(jié)合Redis實現(xiàn)接口冪等性的示例代碼
本文主要介紹了SpringBoot結(jié)合Redis實現(xiàn)接口冪等性的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
使用idea的database模塊繪制數(shù)據(jù)庫er圖的方法
這篇文章主要介紹了使用idea的database模塊繪制數(shù)據(jù)庫er圖,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07
java運行時數(shù)據(jù)區(qū)域和類結(jié)構(gòu)詳解
這篇文章主要介紹了java運行時數(shù)據(jù)區(qū)域和類結(jié)構(gòu),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot集成PostgreSQL并設(shè)置最大連接數(shù)
本文主要介紹了SpringBoot集成PostgreSQL并設(shè)置最大連接數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11

