有人可以清除 spring 中 @GetMapping 和 @PostMapping 之间的区别吗?我们也可以在彼此的地方使用这些注释吗?

Can someone please clear the difference between @GetMapping and @PostMapping in spring also can we use these annotations at one another's place?

例如保存员工的详细信息:

@PostMapping("/url")
public void addEmployee(EmployeeBean emp){
....
}

@GetMapping("/url")
public void addEmployee(EmployeeBean emp){
....
}

我可以这样做吗?

@GetMapping 用于HTTP Get 请求,@PostMapping 用于HTTP Post 请求。 要了解更多信息,请访问以下链接:

https://learnjava.co.in/spring-requestmapping-getmapping-and-postmapping-annotations/

http://engineering.pivotal.io/post/must-know-spring-boot-annotations-controllers/

@PostMapping@RequestMapping(method = RequestMethod.POST) 的快捷方式,@GetMapping@RequestMapping(method = RequestMethod.GET)

的快捷方式

在添加实体的情况下,首选post,因为您可以在正文中传递数据,

@PostMapping("/add")
public void addEmployee(@RequestBody EmployeeBean emp){
....
}

希望对您有所帮助!

All mapping are specialized version of @RequestMapping annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET/POST/PUT/DELETE)

@GetMapping - shortcut for @RequestMapping(method = RequestMethod.GET)
@PostMapping - shortcut for @RequestMapping(method = RequestMethod.POST)
@PutMapping - shortcut for @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping - shortcut for @RequestMapping(method =RequestMethod.DELETE)

@PostMapping – Handle HTTP POST Requests
@GetMapping – Handle HTTP Get Requests
@PutMapping – Handle HTTP Put Requests
@DeleteMapping – Handle HTTP Delete Requests

从技术上讲,您可以在彼此的位置使用注释 但每个注释都是为此目的而创建的。

  • 喜欢处理HTTP Get请求和获取数据的只用 @GetMapping

  • 要执行 add/update 操作,请使用 HTTP POST/PUT 请求,即 @PostMapping 或@PutMapping

  • 要执行删除操作,请使用 HTTP 删除请求,即 @DeleteMapping 注释。