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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With