【问题标题】:Use @PostMapping in place of @GetMapping使用@PostMapping 代替@GetMapping
【发布时间】:2022-11-18 02:33:44
【问题描述】:

如果我使用@PostMapping 获取请求怎么办?

就像我的情况

@PostMapping("/get/{userId}")
public ResponseEntity<UerDto> getUSerById(@PathVariable Integer userId){
    return new ResponseEntity<UserDto>(userService.getUserById(userId),HttpStatus.OK);
}

我需要知道我需要传递哪些参数才能完成这项工作。

我尝试获取 JSON 输出,但遇到错误。

【问题讨论】:

  • 与 GET 相同的参数。

标签: java spring api request-mapping


【解决方案1】:

你遇到了什么样的错误?

如果你想让它与@PostMapping一起工作,我认为你应该在变量的开头添加@PathVariable,你的控制器将是这样的;

 @PostMapping("/get/{userId}")
public ResponseEntity<UserDto> getUserById(@PathVariable Integer userId) {
    UserDto userDto = userService.getUserById(userId);
    return ResponseEntity.ok().body(userDto);
}

【讨论】:

  • 不像我的问题是,在某些地方,如果 getmapping 注释,我们会在适当的位置使用 @postmapping 注释。那么我们如何使用它以及为什么我们使用它而不是使用getmapping。
猜你喜欢
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 2019-04-08
  • 2020-07-30
  • 1970-01-01
  • 2019-08-13
相关资源
最近更新 更多