Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC 3 User Defined Exception handling with User defined View

How to handle user defined exceptions (Custom Exception ex.: "BusinessException") in Spring MVC 3 with custom message and view name ?

For Example :

If i throw my own exception from Service layer it should be caught and should redirect to specified view with message, the view name may be same or different.

I searched in Google, but no luck.

Thanks.

like image 261
Arun R U Avatar asked Feb 19 '23 02:02

Arun R U


2 Answers

Have you checked @ExceptionHandler

for eg :

@ExceptionHandler(MyBusinessException.class)
public ModelAndView handleMyBusinessException(MyBusinessException e) {
   handle it or log it or redirect to error page after populating a model
}

This has advantage of having Exception handled at Spring MVC level itself , you can populate a model and bring up a meaningful error page.

Otherwise you can configure it in web.xml as other answer suggests. But your error page will be more like a static page.

like image 166
Subin Sebastian Avatar answered Feb 20 '23 15:02

Subin Sebastian


You have to propagate exception from service layer to controller layer there you can usedeclarative exception handling and provide exception to view mapping in spring configuration xml

like image 36
user1922329 Avatar answered Feb 20 '23 16:02

user1922329