Spring MVC最佳实践处理控制器中不可恢复的异常 [英] Spring MVC Best Practice Handling Unrecoverable Exceptions In Controller
问题描述
当您有一个控制器执行逻辑与服务和DAO可能会引起不可恢复的异常时,处理这些方法调用的最佳做法是什么?
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?
目前我正在开发的一个应用程序有非常漫长的try catch方法,简单地err.out异常消息,这似乎不是很强大,我认为这个代码闻起来,是否有任何cookie切割器最佳实践在spring-mvc中处理这个?
Currently an app I'm working on has very lengthy try catch methods that simply err.out exception messages, this doesn't seem very robust and I think that this code smells, is there any cookie cutter best practice for handling this in spring-mvc?
推荐答案
它像
@ExceptionHandler(IOException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public void handleExc(IOException ext) {}
这将捕获控制器方法抛出的所有IOExceptions并写出一个500的回应。
That would catch all IOExceptions thrown by the controller method and write out a 500 to the response.
这篇关于Spring MVC最佳实践处理控制器中不可恢复的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!