views:

183

answers:

1

I have annotated controller which contains several methinds mapped 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 json request and do perform some actions. Parsing request may produce checked ParseException which I can handle in method or add throws to its signature. I prefer second approach since in this I don't want additional try/catch in code. So the question is how to configure and code handler for controller methods?

+1  A: 

You should check out 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 subclass AbstractHandlerExceptionResolver and declare it as a spring mvc bean in your xml configuration if you want to handle these exceptions for all your controllers.

Brian Clozel