SpringBoot中API接口參數(shù)獲取方式小結(jié)
引言
在Spring Boot中,API接口參數(shù)可以通過多種方式獲取,具體取決于你定義的API接口參數(shù)類型(如路徑參數(shù)、查詢參數(shù)、請求體參數(shù)、請求頭等)。以下是一些常見的參數(shù)獲取方式以及對應(yīng)的Java樣例代碼:
1.路徑參數(shù)(Path Variable)
使用@PathVariable注解從URL路徑中獲取參數(shù)。
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public ResponseEntity<String> getUserById(@PathVariable Long id) {
// 處理邏輯
return ResponseEntity.ok("User with ID: " + id);
}
}2.查詢參數(shù)(Query Parameter)
使用@RequestParam注解從URL查詢字符串中獲取參數(shù)。
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/search")
public ResponseEntity<String> searchUsers(@RequestParam String name) {
// 處理邏輯
return ResponseEntity.ok("Searching for user with name: " + name);
}
// 也可以設(shè)置默認值
@GetMapping("/searchWithDefault")
public ResponseEntity<String> searchUsersWithDefault(@RequestParam(defaultValue = "John") String name) {
// 處理邏輯
return ResponseEntity.ok("Searching for user with name: " + name);
}
}3.請求體參數(shù)(Request Body)
使用@RequestBody注解從HTTP請求體中獲取參數(shù),通常用于POST或PUT請求。
@RestController
@RequestMapping("/users")
public class UserController {
@PostMapping("/")
public ResponseEntity<String> createUser(@RequestBody User user) {
// 處理邏輯
return ResponseEntity.ok("User created with name: " + user.getName());
}
// 假設(shè)User類如下
static class User {
private String name;
// getters and setters
}
}4.請求頭參數(shù)(Request Header)
通常不直接使用注解來獲取請求頭參數(shù),但可以通過HttpServletRequest對象或@RequestHeader注解來獲取。
使用@RequestHeader注解:
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/")
public ResponseEntity<String> getUsers(@RequestHeader("Authorization") String authToken) {
// 處理邏輯
return ResponseEntity.ok("Authorization token: " + authToken);
}
}使用HttpServletRequest對象:
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/")
public ResponseEntity<String> getUsers(HttpServletRequest request) {
String authToken = request.getHeader("Authorization");
// 處理邏輯
return ResponseEntity.ok("Authorization token from HttpServletRequest: " + authToken);
}
}5.@CookieValue
當我們需要與客戶端保持有狀態(tài)的交互時,就需要用到Cookie。此時,服務(wù)端讀取Cookie數(shù)據(jù)的時候,就可以像下面這樣用@CookieValue來讀取Cookie中的SessionId數(shù)據(jù)。
@GetMapping("/user")
@ResponseBody()
public List<User> getUserList(@CookieValue(name = "SessionId") String sessionId) {
return userRepo.findAll();
}6.@MatrixVariable
這個我們用的并不是很多,但一些國外系統(tǒng)有提供這類API參數(shù),這種API的參數(shù)通過;分割。
比如:這個請求/books/reviews;isbn=1234;topN=5; 就可以如下面這樣,使用@MatrixVariable來加載URL中用;分割的參數(shù)。
@GetMapping("/books/reviews")
@ResponseBody()
public List<BookReview> getBookReviews(
@MatrixVariable String isbn, @MatrixVariable Integer topN) {
return bookReviewsLogic.getTopNReviewsByIsbn(isbn, topN);
}7.表單數(shù)據(jù)(Form Data)
對于POST請求中的表單數(shù)據(jù),通??梢允褂聾ModelAttribute或@RequestParam來獲取。但如果表單數(shù)據(jù)作為請求體發(fā)送(Content-Type: application/x-www-form-urlencoded),則可以直接使用@RequestParam。如果表單數(shù)據(jù)是JSON格式,則應(yīng)使用@RequestBody。
8.Servlet API的其他部分
你還可以使用HttpServletResponse、HttpSession等Servlet API的其他部分來處理請求和響應(yīng)。但在Spring Boot中,通常推薦使用Spring MVC提供的高級抽象來簡化開發(fā)。
注:為了正確解析請求體中的JSON數(shù)據(jù),需要在Spring Boot項目中添加適當?shù)腏SON庫(如Jackson),并且確保請求的Content-Type設(shè)置為application/json。
以上就是SpringBoot中API接口參數(shù)獲取方式小結(jié)的詳細內(nèi)容,更多關(guān)于SpringBoot API接口參數(shù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring Boot場景啟動器(Starters)從入門到精通
本文給大家介紹Spring Boot場景啟動器(Starters)完全指南:從入門到精通,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2025-10-10
SpringBoot實現(xiàn)列表數(shù)據(jù)導(dǎo)出為Excel文件
這篇文章主要為大家詳細介紹了在Spring?Boot框架中如何將列表數(shù)據(jù)導(dǎo)出為Excel文件,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2024-02-02

