# Printing the status of a database insertion as JSON

The controller is currently inserting a record into the database and returning a JSON response as shown below in the try-catch block. I can see the Inserted Successfully within the try-catch block!. Although, I haven't seen this message Error in Inserting Record! so far since my webservice haven't errored out till now.

Please let me know if the approach I have followed to display just two lines of JSON is an efficient / conventional way of doing similar things ? If it can be improved, please suggest.

@RequestMapping(value = "/insertRecord", method = RequestMethod.POST)
public Map<String, String> insertRecord
(
@RequestParam(value = "type_id", defaultValue = "0") Integer type_id,
@RequestParam(value = "tempID", defaultValue = "0") Integer tempID,
@RequestParam(value = "status", defaultValue = "") String successORFailure,
@RequestParam(value = "notes", defaultValue = "") String message
) {

Map<String, String> response =  new HashMap<String,String>();

try {

insertRecordDao insertDao = (insertRecordDao) context.getBean("insertRecordDao");
insertDao.insertAuditInfo(type_id, tempID, successORFailure, message);
response.put("message", "Inserted Successfully within the try-catch block!");
response.put("status", "SUCCESS");

} catch (Throwable th) {
th.printStackTrace();
response.put("message", "Error in Inserting Record!");
response.put("status", "ERROR");

}

return response;

}


Here's a JSON response from my POSTMAN client:

## 2 Answers

This looks like you are trying to create a REST interface. If this is true, the following remarks apply:

• First of all, your path should not include the action. The resource should be named "record" and the action is determined by the HTTP verb, i.e. "insert" is implied by POST, retrieve by GET, modify/update by PUT, etc.
• Success or failure should primarily be communicated via HTTP status. For a POST, customary responses are either code 200 (OK) with the complete object as a result (often modified by the insertion, e.g. having a unique id now) or 204 (no content) to signify that everything is OK without sending the object back.
• Likewise, failure should be communicated as a status code in the 500 range (server error, e.g. if the database connection fails) or 400 range (bad request, e.g. wrong data types transmitted by the client)

Generally, look around for "REST best practices", you'll find lots of information on the topic.

Additionally to @mtj remarks:

• insertRecordDao is obviously a class (or interface). The names classes and interfaces in Java should begin with a capital letter.

• DAOs are usually organized by entity (aka record name, aka database table). If you have a table AuditInfo, then the DAO should be called AuditInfoDao with a method insert (or save).

• Instead of using Spring's ApplicationContext.getBean(), you should be using auto-wiring to inject your dependencies.

• You shouldn't generically catch all Throwables (or Exceptions). This hides programming errors. You should only be catching specific exceptions you know can happen, and handle them properly (at the very least return a useful error message).