Block HTTP POST Request if a record already exists in the database-Collection of common programming errors

I am developing a Java Rest web service to store user data in a MYSQL database.

When I test try POST with curl, I get

204 – No Content

on the client side and

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'vinayUserName' for key 'name_UNIQUE'

on the server side.

How can I program web service to handle 409 conflict for unique data in database and communicate the message to the client?

I am using Jersey to develop Web service and Jquery ajax on the client side to POST the data. Any help is highly appreciated.

Thanks

  1. You should handle SQLException ,Runtime Exception and Exception, then update webservice response with Corresponding Error Message as the parameter(Status – ‘Sucess/Failure’, ErrorMessage – ‘Reason for Failure’).

    In the jquery extract the value from the parameters and display it to user.