-1

What if I use @PostMapping for fetch request instead of @GetMapping? What is the additional thing I need to add to make this API work

Like in my case

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

I need to know what parameters I need to pass to make this work.

I try to get a JSON output, but was encountering an error.

  • 1
    The same parameters as with a GET. – M. Deinum Nov 17 '22 at 08:30
  • NO we can change or add the parameters. All I want to get raw information without effecting any previous data stored in the database. – Avinash gupta Nov 21 '22 at 09:13
  • As I stated the same parameters. What you need for the GET is the same as for the POST there is no difference. If there is an issue you have with this explain that but the parameters are the same. The only change you need is to change `@GetMapping` with `@PostMapping`. You casually mention an error but fail the show/explain what error. – M. Deinum Nov 21 '22 at 09:18

1 Answers1

1

What kind of error did you encounter?

If you want it to work with @PostMapping I think you should add @PathVariable at the beginning of the variable and your controller will be like this;

 @PostMapping("/get/{userId}")
public ResponseEntity<UserDto> getUserById(@PathVariable Integer userId) {
    UserDto userDto = userService.getUserById(userId);
    return ResponseEntity.ok().body(userDto);
}
Umut
  • 11
  • 1
  • No like my questions is that in some places we use @postmapping annotion inplace of getmapping annotation. So how we use it and why we use it instead of using getmapping. – Avinash gupta Nov 17 '22 at 18:34
  • 1
    I am wondering how come you are working on REST API without knowing the differences between standard GET and POST HTTP methods. – Antoniossss Nov 17 '22 at 18:54
  • Ya I Framed my questions in a very wrong way. My bad – Avinash gupta Nov 21 '22 at 09:11