0

If there's only one parameter from the request (for example, memberId received for deleting), should I bind that parameter to Dto or just use it as a primitive?

@DeleteMapping("/members/{memberId}")
ResponseEntity<String> deleteAccount(@PathVariable int memberId){
    memberService.deleteAccount(memberId);
    return ResponseEntity.status(HttpStatus.OK).body("");
}

VS

@DeleteMapping("/members/{memberId}")
ResponseEntity<String> deleteAccount(MemberDeleteDto member){
    memberService.deleteAccount(member);
    return ResponseEntity.status(HttpStatus.OK).body("");
}
Roger
  • 7
  • 3
  • As it is a path variable the second one wouldn't work binding is for either form fields to an object (model attribute) or binding the request body (using @RequestBody). – M. Deinum Aug 16 '21 at 12:15
  • @M.Deinum I tested the codes and both work. So should i use memberId as a primitive(int) all the way to the repository class? – Roger Aug 16 '21 at 12:30
  • Apparently support for URI parameters that match a property on the model object does indeed work (needed to dig into the code a little :) ). So use either, personal preference. – M. Deinum Aug 16 '21 at 12:43
  • Thanks a lot! Have a good day :) – Roger Aug 16 '21 at 12:47

1 Answers1

1

I would say there is no rule for this, but my suggestion would be to keep it simple and use primitives whenever possible for path parameters. Unless you really have a very good reason for using a fully-fledged Object, I would suggest you not to over-engineer it.

João Dias
  • 16,277
  • 6
  • 33
  • 45