0

I am trying to update a resource in DB via @PutMapping (org.springframework.web.bind.annotation.PutMapping) by calling a stored procedure via (java.sql.PreparedStatement.executeUpdate()). This step is working absolutely fine and I am getting below integer response from DB:

  1. 1 - 1 row updated
  2. 0 - No row updated

I want to send the response back to the user on the basis of integer returned from the DB, for that I am returning ResponseEntity<UpdateResponseVO> from the rest controller, where UpdateResponseVO is normal POJO class:

public class UpdateResponseVO {

    private String responseCode;

    private String message;

}

I am framing the ResponseEntity on the basis of below condition:

UpdateResponseVO apiResponse = new UpdateResponseVO();
if (row > 0) {
 apiResponse.setResponseCode("200");
 apiResponse.setMessage("Updated Successfully.");
 return new ResponseEntity<UpdateResponseVO>(apiResponse, HttpStatus.OK);
}
else {
 apiResponse.setResponseCode(???);
 apiResponse.setMessage("No row updated.");
 return new ResponseEntity<UpdateResponseVO>(apiResponse, ???);
}

Now my question is what should be my response code when 0 row gets updated (this condition arises when we are not aware of the current value to be updated in the DB and we give the same value. For e.g in DB already student name is "abc" and we want to update it to "abc" again. Hence row updated will be 0) and what HttpStatus I should send along with the ResponseEntity.

1 Answers1

1

I am not sure where you are handling exception when receiving wrong data, Recommended way to use @Validated and validate your request DTO receiving in your controller body. Use @ControllerAdvice for globally handling exception.
Now, Specifically you should return HttpStatus.FORBIDDEN i.e 403 as this is data validation error. In your case you check if row==0 simple ,In general if there is any processing error then respond back with 500. I personally prefer generic response in Success/Error case with HttpStatus.OK with the use of ResponseEntity<?> where you can return any object in response.

MADHAVI KUMARI
  • 719
  • 1
  • 6
  • 17
  • I am using ControllerAdvice only to handle exceptions at one place. But here there is no error occurred. This condition arises when we are not aware of the current value to be updated in the DB and we give the same value. For e.g in DB already student name is "abc" and we want to update it to "abc" again. Hence row updated will be 0. In this scenario what response code should be returned? – dexterous-unwrapped Jun 16 '21 at 15:34
  • It depends on your application if you classify this situation as exception then from your service method you can throw a custom (DuplicateUpdateException detect using same number of rows updated) exception in that case you should return 403, If you are not classifying it as an exception then you can simply return HttpStatus 200 and `apiResponse.setResponseCode("403");` so that client application can take an action. – MADHAVI KUMARI Jun 16 '21 at 18:20
  • where is DTO where is the error in the question how controller advice solve the question? It is mostly idempotent request where all subsequent calls are same and stored procedure return 0, question wants to know what should be the value in this case. – silentsudo Jun 21 '21 at 05:27