roy_sanu
asked on
issue on Exception handling
Experts,
is it good to have a ParseException or try catch which would be much better way handling...
Thanks
is it good to have a ParseException or try catch which would be much better way handling...
@CrossOrigin
@RequestMapping(value = "save/", method = RequestMethod.POST, produces = { "application/json" })
public Map<String, Object> createCounseling(@RequestBody String counselingData) throws Parse{
JsonObject CounselingObject = Json.parse(counselingData).asObject( );
HibernateDao<Counseling> workerDao = counselingDaoFactory.getNewDao(Couns eling.clas s);
Counseling counseling = getCounselingObject(CounselingObject );
Map<String, Object> counseilingMap = new HashMap<>();
workerDao.save(counseling);
counseilingMap.put("status", HttpStatus.OK);
counseilingMap.put("data",new ArrayList<>());
return counseilingMap;
}
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
2) The factory method / builder is responsible for creating the object(s) from the request body. They should not be separated over different classes.
3) As the createCounseling() method is the endpoint, you should specify via consumes in the @RequestMapping that you only accept application/json. Then parse errors based on non-JSON data cannot happen
4) The factory method can throw an exception if the JSON contains invalid data or you make it exception free, something like:
Open in new window