一、映射 HTTP 請求的處理方法
1、@RestController:
@RestController
是 Spring 4.0 引入的一個注解,它是 @Controller
和 @ResponseBody
的組合。
用于標識一個類,表示這個類是一個控制器,并且其中的方法會返回 JSON 格式的數(shù)據(jù)。通常用于構(gòu)建 RESTful API。
@RestController
public class MyController {
// Controller methods
}
2、@RequestMapping:
@RequestMapping
用于映射 HTTP 請求到控制器的處理方法。
可以用在類級別和方法級別。在類級別上設(shè)置的路徑會與方法級別的路徑拼接起來。
可以指定請求的方法(GET
、POST
等)、路徑、參數(shù)等。
@Controller
@RequestMapping("/example")
public class MyController {
@RequestMapping("/path")
public String myMethod() {
// Method implementation
return "viewName";
}
}
-
指定單個請求方法:
@RequestMapping(value = "/example", method = RequestMethod.GET) public String handleGetRequest() { // 處理GET請求 return "GET response"; }
上面的例子中,
handleGetRequest
方法只處理HTTP GET請求。 -
指定多個請求方法:
@RequestMapping(value = "/example", method = {RequestMethod.GET, RequestMethod.POST}) public String handleGetOrPostRequest() { // 處理GET或POST請求 return "GET or POST response"; }
在 method 屬性中使用數(shù)組,可以指定多個請求方法。
-
傳參
@RequestMapping
注解通常不直接支持在運行時動態(tài)地傳遞參數(shù)。@RequestMapping
注解是一個元注解,它用于組合其他注解,而這些其他注解(例如@RequestParam
、@PathVariable
)才提供了在運行時動態(tài)獲取請求參數(shù)的功能。例如@RequestMapping(value = "/example", method = RequestMethod.GET) public String handleRequest(@RequestParam String paramName) { return "Response with parameter: " + paramName; }
3、@GetMapping:
@GetMapping
是 @RequestMapping(method = RequestMethod.GET)
的縮寫。用于處理 HTTP GET 請求的方法級注解。
@Controller
@RequestMapping("/example")
public class MyController {
@GetMapping("/path")
public String myMethod() {
// Method implementation
return "viewName";
}
}
4、@PostMapping:
@PostMapping
是 @RequestMapping(method = RequestMethod.POST)
的縮寫。用于處理 HTTP POST 請求的方法級注解。
@Controller
@RequestMapping("/example")
public class MyController {
@PostMapping("/path")
public String myMethod() {
// Method implementation
return "viewName";
}
}
這些注解一起使用,可以創(chuàng)建具有清晰結(jié)構(gòu)的RESTful風(fēng)格的控制器。@RequestMapping
提供了靈活的選項,而 @GetMapping
和 @PostMapping
則是對特定 HTTP 方法的簡化表示。@RestController
則簡化了 @Controller
和 @ResponseBody
的組合,使代碼更加簡潔。
二、處理了參數(shù)的注解
下面是一個簡單的例子,演示了如何結(jié)合使用 @RequestMapping、@PathVariable
和 @RequestParam
:
@RestController
@RequestMapping("/example")
public class ExampleController {
@GetMapping("/path/{id}")
public String handlePathWithPathVariable(@PathVariable Long id) {
return "Response from path with id: " + id;
}
@GetMapping("/query")
public String handleQueryParameter(@RequestParam String param) {
return "Response from query parameter: " + param;
}
@PostMapping("/body")
public String handleRequestBody(@RequestBody YourRequestBodyClass requestBody) {
return "Response from request body: " + requestBody.toString();
}
}
在這個例子中:
@GetMapping("/path/{id}")
使用了@PathVariable
來獲取路徑中的變量 id 的值。@GetMapping("/query")
使用了 @RequestParam
來獲取請求中的查詢參數(shù)。@PostMapping("/body")
使用了 @RequestBody
來獲取請求體中的數(shù)據(jù)。
這種結(jié)合使用的方式使得你可以方便地從不同的位置獲取請求參數(shù),根據(jù)請求的不同部分,如路徑、查詢參數(shù)或請求體來進行處理。
@RequestParam
、@PathVariable
、和 @RequestBody
是 Spring MVC 中用于獲取請求參數(shù)的三個常用注解,它們分別用于不同的場景:
1、@RequestParam:
用于從請求的查詢參數(shù)中獲取值。通常用于處理表單提交或URL中的查詢參數(shù)。
@RequestMapping("/example")
public String handleRequest(@RequestParam String paramName) {
return "Response with parameter: " + paramName;
}
請求路徑:/example?paramName=value
2、@PathVariable:
用于從請求路徑中獲取值。通常用于處理 RESTful 風(fēng)格的URL,其中某些值是路徑變量。
@RequestMapping("/example/{id}")
public String handleRequest(@PathVariable Long id) {
return "Response with id: " + id;
}
請求路徑:/example/123
3、@RequestBody:
用于從請求體中獲取數(shù)據(jù)。通常用于處理 POST 或 PUT 請求中的 JSON 數(shù)據(jù)。
@RequestMapping(value = "/example", method = RequestMethod.POST, consumes = "application/json")
public String handleRequest(@RequestBody YourRequestBodyClass requestBody) {
return "Response with request body: " + requestBody.toString();
}
在這里,YourRequestBodyClass
應(yīng)該是一個與請求體JSON格式匹配的Java類。Spring會自動進行反序列化。文章來源:http://www.zghlxwxcb.cn/news/detail-795328.html
4、總結(jié)一下:
@RequestParam
用于獲取請求的查詢參數(shù)。@PathVariable
用于獲取請求路徑中的變量。@RequestBody
用于獲取請求體中的數(shù)據(jù)。
這三個注解通常根據(jù)請求的類型和參數(shù)位置來選擇使用。根據(jù)具體的業(yè)務(wù)需求,你可以選擇其中的一個或多個來獲取請求參數(shù)。文章來源地址http://www.zghlxwxcb.cn/news/detail-795328.html
到了這里,關(guān)于【注解】@RestController、@GetMapping、@PostMapping、@RequestMapping等RESTful風(fēng)格的Web服務(wù)的注解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!