Spring MVC: best way to handle exception for Ajax request and regular request?

I want to define a common exception box in my project, so I use @ControllerAdvice to do the code snippet below:

@ExceptionHandler(Exception.class) public ModelAndView handleAllException(HttpServletRequest request, Exception ex) throws Exception { LOGGER.error(ex.getMessage()); ModelAndView mav = new ModelAndView(); mav.addObject("exception", ex); mav.addObject("url", request.getRequestURL()); mav.setViewName(ViewConstants.INTERNAL_ERROR_VIEW); return mav; } 

it will return a common error page. This is great for a normal query exception. But if it's an Ajax request, the result is so ugly. so I am adding code to judge this. The added code is shown below:

 if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) { // return HTTP Status code and response message } else { // return error page name } 

I don’t think this is the best way, does anyone have a good opinion?

+5
source share
4 answers

I have all my controllers in different packages, based on whether they serve AJAX requests or not. Then I can set the #basePackages element in the ControllerAdvice annotations to handle the corresponding exception

Update: See RequestMapping # params and RequestMapping # headers for separation based on controllers on headers and / or parameters

+1
source

I would suggest setting an error response code on any request, think that it is good practice to notify the client that something will go wrong, depending on the type of request. And for an ajax request, you can return the same page and identify the problem by the error code.

0
source

If you use jQuery to execute queries, you can use the following:

 jQuery.ajaxSetup({ headers: { 'ajax-request': true }, statusCode: { 400: function (xhr) { ...do something }, 500: function (xhr) { ...do something } ... } }); 
0
source
 ... public class Application extends SpringBootServletInitializer { @Bean(name = "simpleMappingExceptionResolver") public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver(); r.setDefaultErrorView("forward:/errorController"); return r; } 

 @Controller public class ErrorController { public static final Logger LOG = Logger.getLogger(ErrorController.class); @RequestMapping(value = "/errorController") public ModelAndView handleError(HttpServletRequest request, @RequestAttribute("exception") Throwable th) { ModelAndView mv = null; if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) { if (isBusinessException(th)) { mv = new ModelAndView("appAjaxBadRequest"); mv.setStatus(BAD_REQUEST); } else { LOG.error("Internal server error while processing AJAX call.", th); mv = new ModelAndView("appAjaxInternalServerError"); mv.setStatus(INTERNAL_SERVER_ERROR); } mv.addObject("message", getUserFriendlyErrorMessage(th).replaceAll("\r?\n", "<br/>")); } else { LOG.error("Cannot process http request.", th); mv = new ModelAndView("appErrorPage"); mv.addObject("exeption", th); } return mv; } } 
0
source

All Articles