My code has a nasty smell that looks like this:@RequestMapping(value = { /updateGraph }, method = RequestMethod.POST)public ResponseEntity<ArrowsGraph> updateGraph(@RequestBody ArrowsGraph g){    try{        g = arrowsBo.updateGraph(g);                    return new ResponseEntity<ArrowsGraph>(g, HttpStatus.OK);    } catch (Exception e) {        errorLogging.logFatalException(e,  sessionState);        return new ResponseEntity<ArrowsGraph>(HttpStatus.INTERNAL_SERVER_ERROR);    }}@RequestMapping(value = { /deleteGraph }, method = RequestMethod.POST)public ResponseEntity<ArrowsGraph>  deleteGraph(@RequestBody ArrowsGraph g){    try{        g = arrowsBo.deleteGraph(g);        return new ResponseEntity<ArrowsGraph>(g, HttpStatus.OK);    } catch (Exception e) {        errorLogging.logFatalException(e,  sessionState);        return new ResponseEntity<ArrowsGraph>(HttpStatus.INTERNAL_SERVER_ERROR);    }}Things to mention:The exceptions being caught are runtime exceptions, likely thrown if the database mapping is configured incorrectly. If the code is working correctly, exceptions should never be thrown, but in the case there is an error, I want to display an error the client side, instead of having unintended behaviour. The reason I'm using ResponseEntity<..> instead of @ResponseBody is so I can return a 500 in the case that an exception is thrown.The purpose of errorLogging.logFatalException(...) is to log the error, and notify admins.Before I refactored to use ResponseEntity<...> and catch the runtime exceptions, the methods looked a lot cleaner: @RequestMapping(value = { /deleteGraph }, method = RequestMethod.POST)public ArrowsGraph  deleteGraph(@RequestBody ArrowsGraph )     return arrowsBo.deleteGraph(g);}Is there a nice way to avoid this repetition/boilerplate?
Spring MVC controller
java;error handling;spring mvc
Is there a nice way to avoid this repetition/boilerplate?Yes, there is a cleaner way.Since you are using Spring, please check the annotation @ControllerAdvice and the class ResponseEntityExceptionHandler.To handle exceptions, you'll need to extend ResponseEntityExceptionHandler and manage the returned ResponseEntity in a method annotated with @ExceptionHandler, for example:@ControllerAdvice(basePackageClasses = MyControllerClass.class)public class ErrorResponseHandler extends ResponseEntityExceptionHandler {  @ExceptionHandler(MyExceptionType.class)  @ResponseBody  ResponseEntity<Object> handleControllerException(HttpServletRequest request, Throwable ex) {    // process the exception and wrap the response to return in ResponseEntity  }}Using this approach, you service methods will remain one-liners, returning non-wrapped ArrowsGraph instances.