Spring MVC ExceptionHandler gets easier.


Spring MVC exception handling gets easier with @ControllerAdvice with the release of Spring 3.2.1.

You can annotate your class as shown below and add a method to handle exception


@ControllerAdvice
public class AppCoreController
{
@ExceptionHandler(Exception.class)
public @ResponseBody RequestStatus handleException(Exception exception)
{
RequestStatus errorStatus = new RequestStatus();
errorStatus.setMessageCode(-999);
errorStatus.setMessageDescription(exception.getMessage());
return errorStatus;
}
}


Please note that RequestStatus is a custom controller and yeap we are returning an class / object type as oppose to just String data type. 

These add exception handling capabilities to all your existing controller. 


Comments

Popular posts from this blog

OpenCover code coverage for .Net Core

ionic2 cordova build android - Unable resolve gradle 2.2.3

Using Custom DLL with IronPython / Scripts