r/SpringBoot • u/delusionalbreaker • Dec 26 '25
Question Is using @PostMapping for deleting an user account better than @DeleteMapping when you have payloads?
Some context im fairly new to springboot and i have made 2 projects in it (1 small sized and 1 medium ish) right now im working on my 3rdproject which is an e-commerce backend in springboot along with mysql as database.
So my question arises from a confusion im facing regarding user deletion mapping
my service method for deletion of an user's account looks like this:
@Override
@Transactional
public String deleteUser(UserDeleteRequest request) {
// we get the current user as only you are able to delete your own acc
User currentUser = currentUser();
if (!passwordEncoder.matches(request.getUserPassword(), currentUser.getPassword())) {
throw new InvalidPasswordException("Invalid Password");
}
// if everything's alright we delete it now
userRepository.delete(currentUser);
return "User Successfully Deleted!";
}
and my controller mapping for that method looks like this:
@Operation(summary = "Delete user's account", description = "Delete current user's account")
@DeleteMapping("/delete")
public ResponseEntity<String> deleteUser(
(description = "payload for deleting account") UserDeleteRequest request) {
String response = userService.deleteUser(request);
return new ResponseEntity<>(response, HttpStatus.OK);
}
so that UserDeleteRequest DTO contains user's current password which user has to type so that account can be deleted but then i learn't its not recommend to send anything with the delete mapping so i was wondering should i use PostMapping in such case? whats mapping is used for such cases in enterprise applications?
Edit:- Many of you seem to misunderstand that i store my password in plain text which is not the case my passwords are hashed and encrypted using bcrypt inside the database while my jwt token provides the user's email which is then checked against the database
Edit 2:- Thanks for the replies guys i decided to use post mapping for my scenario also many of you seem to misunderstand that whay i was using password whennuser is already authenticated, well it just just as an final confirmation from user to delete thier account and rather than an random string i wanted it to be more secure so i thought users password would be a great idea here. Anyways thanks for your advices ill be sure to make another post when i complete the project so you guys can review it and provide more advices. Thanks! đ
6
u/ashdgjklashgjkdsahkj Dec 26 '25 edited 3d ago
The original post content no longer exists here. The author used Redact to remove it, exercising their right to control their data & privacy.
Deserve butter compass exultant long office water kettle
7
u/two_wheel_soul Dec 26 '25
everything is recommended, not hard binding... u can always send anything with any verb.
depends on u.. what u prefer.. common understanding over technical understanding.
Right question would be ... Why user needs to send password to uservice when he just wants to delete.
is it for double confirmation n hard check? the implement another api which sets some flag that deleteConfirmed with ttl of 5 min or so.
PS: didnt read ur code, just ur last paragraph
1
u/HellaSwellaFella Dec 26 '25
Doesn't matter that much functionally but use whatever is recommended and for convention
A lot of the times it's less about what "works" and what "makes sense"
Using correct annotations declares intention, if a different developer joins you or you come to your codebase in a month it will help you to know that you're using deletemapping for deletion
1
u/DominusEbad Dec 26 '25
What is in UserDeleteRequest? If it's just a password, why are they sending a password? I saw some of your other responses and you state you authenticate with a JWT, so why require a password?
It's somewhat code smell requiring a password like this if you are already authenticating a JWT.
You should require a resource ID in your path and trust the JWT.
Then you can include the userId as claim in your JWT and perform a quick authorization by comparing the path userId with the claim userId in a resolver, or even use @PreAuthorize and have it compare the JWT userId and resource userId automatically in your controller.Â
Then you don't need to worry about a request body at all and use @DeleteMapping
1
u/arca9147 Dec 27 '25
You can send a body within a delete request, thats ok, but asking for password? Either force https or use tokens if what you want is to ensure user identity or a role based access control with permissions and validating if user is authorized.
Tl:dr it is not, use deletmapping with body, stay consistent between naming an action
0
u/Agile_Rain4486 Dec 26 '25
whatever is fine man, heck you could even use get mapping and it would work the same. These are more like markers, fine in personal projects with 0 consequences but in production your team lead would call it out.
0
u/gozluklumarti Dec 27 '25
I would not recommend sending a payload with a delete request. Kind of an anti pattern, if you ask me. Instead of forcing users to type in their password, you can send a verification email with a link. When the user clicks the link, you can do the deletion operation. This adds an extra layer of security, since delete operation is most probably final and you will lose user data forever.
1
u/amhang Dec 27 '25
Use PUT instead.
1
u/titanium_mpoi Dec 27 '25
Stick to REST guidelines.