Spring MVC Rest Service Controller with Error Handling done right?

后端 未结 5 774
一向
一向 2020-12-08 15:06

I was wondering how to correctly implement a Spring Controller which is supposed to serve as a REST Service. Especially I want to try and make the interface as RESTful as po

5条回答
  •  余生分开走
    2020-12-08 15:32

    If you want your whole Exception with stackTrace to be transmitted toward your client, as @Bart said you should send an "ErrorResource" object.

    A library has it off-the-shelf :

    
      com.github.zg2pro
      spring-rest-basis
      0.2
    
    

    add it to your project and then just add a "@ControllerAdvice" class to your beans, as it is explained in the project wiki.

    That should handle your errors nicely!

提交回复
热议问题