Spring-mvc controller and exception handling

后端 未结 5 1472
星月不相逢
星月不相逢 2021-02-09 08:35

Would like to ask you a best practice question where a spring-mvc controller is concerned. Please review the code below:

    @Autowired
    SomeService service;
         


        
5条回答
  •  醉话见心
    2021-02-09 09:15

    A good practice with exception handling is to throw early and catch late. In your case, that would mean catching the error at the controller instead of the service. The advantage here is that you can code different controllers based on the client request (SOAP/REST/JSON...), to handle the exceptions differently. But if that logic is in the service, you have less flexibility over how to handle the return from the service, in your response to different clients.

提交回复
热议问题