HandlerExceptionResolver for annotated controller

I have an annotated controller that contains several tags displayed on URLs. Like this:

@Controller public class CategoryController { @RequestMapping(value = "/addCategories") public void addCategories(@RequestParam(value = "data") String jsonData) throws ParseException @RequestMapping(value = "/getNext") public void getNext(@RequestParam(value = "data") String jsonData) throws ParseException ... } 

Methods need to parse the json request and perform some actions. The Parsing request may specify a ParseException , which I can handle in the method or add throws to my signature. I prefer the second approach, because in this I do not want to add try / catch code to the code. So, the question is how to configure and process the code for controller methods?

+4
source share
1 answer

You should check the spring documentation for @ExceptionHandler .

 @Controller public class CategoryController { @ExceptionHandler(ParseException.class) public ModelAndView handleParseExc(ParseException ex) { //... } @RequestMapping(value = "/addCategories") public void addCategories(@RequestParam(value = "data") String jsonData) throws ParseException } 

Or a subclass of AbstractHandlerExceptionResolver and declare it as spring mvc bean in your xml configuration if you want to handle these exceptions for all your controllers.

+3
source

All Articles