Java – spring MVC controller and exception handling
I'd like to ask a best practice question about spring MVC controller Please check the following code: @ h_ 403_ 2@@Autowired SomeService service; @ RequestMapping (...) public @ResponseBody Response createSomething () { try { serviceResponse = service.doSomething(); //create a success response and return } catch (SomeServiceException e) { //create an error response and return } }
Is error handling normal at the controller level? Or whether the service class should throw an exception as shown above Please review and tell me
Solution
I would say you have three strategies, depending on your use case
There are roughly three strategies: handlerexceptionresolver, @ exceptionhandler, and handle exceptions inside the operation
These use cases include: common exception handlers for the entire application, the entire controller, and corresponding specific operations