Spring MVC Best Practice Handling Unrecoverable Exceptions In Controller

后端 未结 2 1599
梦毁少年i
梦毁少年i 2020-12-17 06:06

When you have a controller that does logic with services and DAO\'s that may throw an unrecoverable exception, what is the best practice in dealing with those method calls?<

2条回答
  •  执笔经年
    2020-12-17 07:09

    Check out the @ExceptionHandler

    You can use it like

    @ExceptionHandler(IOException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public void handleExc(IOException ext) {}
    

    That would catch all IOExceptions thrown by the controller method and write out a 500 to the response.

提交回复
热议问题