return only http status from the controller

hey guys. i want to ask for some help. i have a controller and i want it to return only http status and no body. can i do it this way?
@PostMapping(value = "/createPaymentConfirmation", produces = "application/json")
public ResponseEntity createPaymentConfirmation(@RequestBody @Valid PaymentDTO paymentConfirmationRequest) {
paymentService.createPaymentpaymentConfirmation(paymentConfirmationRequest);
ResponseEntity response = ResponseEntity.ok().build();
return response;
}
@PostMapping(value = "/createPaymentConfirmation", produces = "application/json")
public ResponseEntity createPaymentConfirmation(@RequestBody @Valid PaymentDTO paymentConfirmationRequest) {
paymentService.createPaymentpaymentConfirmation(paymentConfirmationRequest);
ResponseEntity response = ResponseEntity.ok().build();
return response;
}
it works, but i was wondering maybe smth can be done better? thx in advance
10 Replies
JavaBot
JavaBot6mo ago
This post has been reserved for your question.
Hey @bambyzas! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
tjoener
tjoener6mo ago
Just return void By default it will be a 200, unless authentication is wrong (4xx) or there's an exception (5xx)
0x150
0x1506mo ago
yeah thats the officially endorsed way
bambyzas
bambyzasOP6mo ago
i was told i can return ResponseEntity<Void> too or put @ResponseStatus(HttpStatus.NO_CONTENT) and make my controller method return void
0x150
0x1506mo ago
if you just want to return 200 or 204, then just return void and do nothing
bambyzas
bambyzasOP6mo ago
actually i need to return 204 No Content
0x150
0x1506mo ago
yeah thats what i meant i forgot the code
bambyzas
bambyzasOP6mo ago
ok
tjoener
tjoener6mo ago
This If you have a restcontroller, no need to put the produces in there either
JavaBot
JavaBot6mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?