I know the difference between @PathVariable
and @RequestParam
, that's not the point.
I have also read some articles but I still don't understand when to use one or the other in a simple case.
For a REST API, let's say I have a User
entity with id
and username
(both are unique).
Now I want to get a user by username.
I have 2 options :
@GetMapping(path = "/users/{username}")
public ResponseEntity<Object> getUser(@PathVariable String username){
//Get user
}
or
@GetMapping(path = "/users}")
public ResponseEntity<Object> getUser(@RequestParam String username){
//Get user
}
Which one do I have to use (@RequestParam is not mandatory in the code, I placed it just to be clearer) ?
Thanks