Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
change tags & modify question to make it more readable
Source Link

Why is Spring exception handler is not working as expected

When I using spring exception handler based on controlleruse @ExceptionHandler within Controller, it's not working as expected.

Here is my code is here:

@Controller public class PageController { @RequestMapping("/page") public ModelAndView index(ModelAndView modelAndView){ String mess = null; mess.split(","); modelAndView.setViewName("index"); return modelAndView; } @ExceptionHandler({Exception.class}) @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Bad Request") public ModelAndView handleException(Exception ex, HttpServletRequest request, HttpServletResponse response){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", ex.getMessage()); modelAndView.addObject("url", request.getRequestURL()); modelAndView.addObject("code", response.getStatus()); modelAndView.setViewName("exception"); return modelAndView; } } 

afterAfter the application startedstarts with debug mode, I visit http://localhost:8080/pagehttp://localhost:8080/page, and handleException has beenhandleException is running, but the view is below other than the expected excepction view.Why Why? enter image description here

enter image description here

Spring exception handler is not working

When I using spring exception handler based on controller, it's not working.

my code is here:

@Controller public class PageController { @RequestMapping("/page") public ModelAndView index(ModelAndView modelAndView){ String mess = null; mess.split(","); modelAndView.setViewName("index"); return modelAndView; } @ExceptionHandler({Exception.class}) @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Bad Request") public ModelAndView handleException(Exception ex, HttpServletRequest request, HttpServletResponse response){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", ex.getMessage()); modelAndView.addObject("url", request.getRequestURL()); modelAndView.addObject("code", response.getStatus()); modelAndView.setViewName("exception"); return modelAndView; } } 

after application started with debug mode, visit http://localhost:8080/page, and handleException has been running, the view is below other than expected.Why? enter image description here

Why is Spring exception handler not working as expected

When I use @ExceptionHandler within Controller, it's not working as expected.

Here is my code:

@Controller public class PageController { @RequestMapping("/page") public ModelAndView index(ModelAndView modelAndView){ String mess = null; mess.split(","); modelAndView.setViewName("index"); return modelAndView; } @ExceptionHandler({Exception.class}) @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Bad Request") public ModelAndView handleException(Exception ex, HttpServletRequest request, HttpServletResponse response){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", ex.getMessage()); modelAndView.addObject("url", request.getRequestURL()); modelAndView.addObject("code", response.getStatus()); modelAndView.setViewName("exception"); return modelAndView; } } 

After the application starts with debug mode, I visit http://localhost:8080/page, and handleException is running, but the view is below other than the expected excepction view. Why?

enter image description here

Source Link

Spring exception handler is not working

When I using spring exception handler based on controller, it's not working.

my code is here:

@Controller public class PageController { @RequestMapping("/page") public ModelAndView index(ModelAndView modelAndView){ String mess = null; mess.split(","); modelAndView.setViewName("index"); return modelAndView; } @ExceptionHandler({Exception.class}) @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Bad Request") public ModelAndView handleException(Exception ex, HttpServletRequest request, HttpServletResponse response){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", ex.getMessage()); modelAndView.addObject("url", request.getRequestURL()); modelAndView.addObject("code", response.getStatus()); modelAndView.setViewName("exception"); return modelAndView; } } 

after application started with debug mode, visit http://localhost:8080/page, and handleException has been running, the view is below other than expected.Why? enter image description here