RequestMapping和GetMapping以及PostMapping
在網上流行的springboot的helloworld里,由這么一段:
@RestController @RequestMapping("/api") public class GreetingController { @GetMapping("/hello") public Object sayHello() { return "hello"; } @GetMapping("/greeting/{name}") public String greetUser(@PathVariable String name) { return "?? Hello, " + name + "! Welcome to Spring Boot!"; } }
RequestMapping的完整寫法是:
RequestMapping(value=xxx, method=xxx)
例如RequestMapping(value="/hello", method=RequestMethod.GET),這就相當于下面的:
@GetMapping("/hello")
后者可以看作包裝了前者,寫起來更方便。
RequestMapping省略method參數時,表示兩種method都被接受并在此處理。

浙公網安備 33010602011771號