Question

I am throwing exception in a scenario. Which is handled by @ExceptionHandler. But when throwing exception it says Request method 'POST' not supported
Controller code

@RequestMapping(value = "abcd", method = {RequestMethod.POST,RequestMethod.GET })
public String testAbc(Model model, HttpServletRequest request) throws Exception {
    //some piece of code
    if(someCondition)
        throw new Exception("No data found with id ");
}

Code in ExceptionController class

@ExceptionHandler(Exception.class)
public ModelAndView handleException(Exception ex, HttpServletRequest request, HttpServletResponse response) {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("errorMessage", ex.getMessage());
    modelAndView.addObject("errorDetails", ExceptionUtils.getStackTrace(ex));
    modelAndView.setViewName("forward:errorPage");

    return modelAndView;
}

Have no idea what I am doing wrong.

Was it helpful?

Solution

It seems that the controller that handles /errorPage does not take request method POST. In your @ExceptionHandler method, you are doing a forward to that page by setting view name to forward:errorPage.

Can you confirm if errorPage controller does handle POST method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top