Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request method 'POST' not supported when throwing exception

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.

like image 206
sandy Avatar asked Dec 19 '12 08:12

sandy


People also ask

Which of the given HTTP mapping methods spring rest doesn't support?

Spring declares all the supported request methods under an enum, RequestMethod, which specifies the standard GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, and TRACE verbs. The Spring DispatcherServlet supports all of them by default, except OPTIONS and TRACE.


1 Answers

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.

like image 110
Viral Patel Avatar answered Nov 14 '22 22:11

Viral Patel