javascript
Spring 3.2 @ControllerAdvice批注的异常处理
不久前,我寫了一個(gè)博客,概述了如何將Spring示例代碼升級(jí)到3.2版,并演示了其中的一些小“陷阱”。 從那以后,我一直在仔細(xì)閱讀Spring 3.2的新功能列表,盡管它不包含任何革命性的新更改,但我懷疑Spring的家伙正在為版本4保存,它確實(shí)包含了一些簡潔的升級(jí)。 引起我注意的第一個(gè)是新的@ControllerAdvice批注,它似乎巧妙地填補(bǔ)了Spring 3功能的空白。 讓我解釋…
如果您看一下我關(guān)于Spring 3 MVC異常處理程序的博客,您會(huì)看到該示例代碼包含一個(gè)帶有請(qǐng)求處理程序方法的易碎控制器,該請(qǐng)求處理程序方法拋出
IOException 。 IOException然后由同一方法中的另一個(gè)方法處理 帶有@ExceptionHandler(IOException.class)注釋的控制器。 問題是,用@ExceptionHandler(IOException.class)注釋的方法只能處理其包含的控制器拋出的IOException 。 如果要?jiǎng)?chuàng)建一個(gè)全局異常處理程序來處理所有控制器拋出的異常,則必須還原到諸如Spring 2的SimpleMapingExceptionHandler和一些XMLconfiguration之類的內(nèi)容 。 現(xiàn)在情況有所不同。 為了演示@ControllerAdvice的用法,我創(chuàng)建了一個(gè)簡單的Spring 3.2 MVC應(yīng)用程序,您可以在github上找到它。 該應(yīng)用程序的主頁表面上允許用戶顯示其地址或信用卡詳細(xì)信息,
…除非用戶嘗試執(zhí)行此操作,否則關(guān)聯(lián)的控制器將拋出一個(gè)
IOException ,應(yīng)用程序顯示以下錯(cuò)誤頁面:
生成異常的控制器非常簡單,并在下面列出:
@Controller public class UserCreditCardController {private static final Logger logger = LoggerFactory.getLogger(UserCreditCardController.class);/*** Whoops, throw an IOException*/@RequestMapping(value = "userdetails", method = RequestMethod.GET)public String getCardDetails(Model model) throws IOException {logger.info("This will throw an IOException");boolean throwException = true;if (throwException) {throw new IOException("This is my IOException");}return "home";}}@Controller public class UserAddressController {private static final Logger logger = LoggerFactory.getLogger(UserAddressController.class);/*** Whoops, throw an IOException*/@RequestMapping(value = "useraddress", method = RequestMethod.GET)public String getUserAddress(Model model) throws IOException {logger.info("This will throw an IOException");boolean throwException = true;if (throwException) {throw new IOException("This is my IOException");}return "home";}}如您所見,此代碼所做的全部工作就是將userdetails和useraddress映射到getCardDetails(...)和getUserAddress(...)方法。 當(dāng)這些方法之一拋出IOException ,以下類將捕獲該異常:
@ControllerAdvice public class MyControllerAdviceDemo {private static final Logger logger = LoggerFactory.getLogger(MyControllerAdviceDemo.class);@Autowiredprivate UserDao userDao;/*** Catch IOException and redirect to a 'personal' page.*/@ExceptionHandler(IOException.class)public ModelAndView handleIOException(IOException ex) {logger.info("handleIOException - Catching: " + ex.getClass().getSimpleName());return errorModelAndView(ex);}/*** Get the users details for the 'personal' page*/private ModelAndView errorModelAndView(Exception ex) {ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("error");modelAndView.addObject("name", ex.getClass().getSimpleName());modelAndView.addObject("user", userDao.readUserName());return modelAndView;} }上面的類由新的@ControllerAdvice注釋注釋,并且包含單個(gè)公共方法handleIOException(IOException.class) 。 此方法捕獲上面的控制器拋出的所有IOException,生成包含一些相關(guān)用戶信息的模型,然后顯示和錯(cuò)誤頁面。 這樣做的好處是,無論您的應(yīng)用程序包含多少個(gè)控制器,當(dāng)任何一個(gè)控制器拋出IOException ,它將由MyControllerAdviceDemo異常處理程序進(jìn)行處理。
@ModelAttribute和@InitBinder要記住的最后一件事是,盡管ControllerAdvice批注對(duì)于處理異常很有用,但也可以在全局處理@ModelAttribute和@InitBinder批注時(shí)使用它。 ControllerAdvice和@ModelAttribute的組合使您能夠在一處為所有控制器設(shè)置模型對(duì)象,同樣, ControllerAdvice和@InitBinder的組合使您可以在同一位置將相同的自定義驗(yàn)證器附加到所有控制器。
參考:來自Captain Debug博客博客的JCG合作伙伴 Roger Hughes 提供的Spring 3.2 @ControllerAdvice注釋的異常處理 。
翻譯自: https://www.javacodegeeks.com/2013/03/exception-handling-with-the-spring-3-2-controlleradvice-annotation.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Spring 3.2 @ControllerAdvice批注的异常处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓软件网站分享(安卓软件网站)
- 下一篇: 拷贝文件Linux命令(拷贝文件 lin