I have decided that the best approach for my scenario, where I only need to fetch the user id and then respond back with it, is to use the @RequestHeader("userId") Long userId annotation.
Let's have a look at how I had configured the enpoint initially:
@PostMapping(path = "/add-follower/{userIdForFollowing}/{currentUserId}")
public ResponseEntity<String> addFollower(@PathVariable ("userIdForFollowing") Long userIdForFollowing, @PathVariable Long currentUserId)
{
Follow newFollow = followService.returnNewFollow(userIdForFollowing, currentUserId);
newFollow = followService.saveFollowToDb(newFollow);
return new ResponseEntity<>("Follow saved successfully", HttpStatus.OK);
}
Now, let's look at how I refactored the endpoint to fetch the id's from the header and return them in the response:
@PostMapping(path = "/add-follower")
public ResponseEntity<String> addFollower(@RequestHeader("userIdForFollowing") Long userIdForFollowing, @RequestHeader("currentUserId") Long currentUserId)
{
Follow newFollow = followService.returnNewFollow(userIdForFollowing, currentUserId);
newFollow = followService.saveFollowToDb(newFollow);
//here I will add more code which should replace the String in the ResponseEntity.
return new ResponseEntity<>("Follow saved successfully", HttpStatus.OK);
}