欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 前端技术 > javascript >内容正文

javascript

controller需要捕获异常吗-凯发k8官方网

发布时间:2024/10/8 javascript 0 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 controller需要捕获异常吗_spring之controller异常处理 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

在spring web后端开发中,对于controller方法的异常一般都需要特别处理,以防止将异常信息抛给前端或用户。但是如果在各个controller方法中通过try-catch来捕获处理,不仅繁琐而且代码也不够简洁优雅。这里我们介绍如何通过@exceptionhandler、@controlleradvice注解实现对controller方法异常的统一处理

@exceptionhandler 异常处理器注解

该注解标注的方法(即异常处理器),可以对其所在类的中的所有controller方法(即@requestmapping注解标注的方法)抛出的异常进行拦截,以便统一处理。同时可在该注解上指定所需捕获的异常类型。同时该方法也支持通过添加@responsebody注解来向前端返回请求的响应结果。下述代码即是一个该注解的使用实例

@controller @requestmapping("student") public class studentcontroller { /*** 异常处理器* @param e* @return*/@responsebody // 通过异常处理器方法统一返回响应结果@exceptionhandler(exception.class)public string handleexception1(exception e) {string msg = "get exception in handleexception1 method";system.out.println("msg: " msg);return msg;}/*** 运算异常处理器* @param e* @return*/@responsebody // 通过异常处理器方法统一返回响应结果@exceptionhandler(arithmeticexception.class)public string handleexception2(arithmeticexception e) {string msg = "get arithmeticexception in handleexception2 method";system.out.println("msg: " msg);return msg;}@responsebody@requestmapping("/test1")public integer test1(@requestparam int a) {int b = 10 / a;system.out.println("b: " b);return b;} }
  • 当通过postman向 http://localhost:8088/student/test1?a=tony 发送请求,controller方法由于方法参数类型错误而抛出异常,然后该异常被传递到异常处理器 handleexception1 方法中统一进行处理
  • 当通过postman向 http://localhost:8088/student/test1?a=0 发送请求,controller方法由于除0而抛出arithmeticexception运算异常。controller方法中抛出的异常会被与异常处理器所指定拦截的异常类型继承关系最近的异常处理器方法所拦截,所以,该运算异常被传递到运算异常处理器 handleexception2 方法中统一进行处理
  • 当通过postman向 http://localhost:8088/student/test1?a=2 发送请求,controller方法未抛出异常,方法按正常流程执行并返回预期结果

@controlleradvice 控制器通知注解

虽然异常处理器大大方便了我们对于controller方法中异常的处理,但是通常在一个项目中有多个controller类,如果在每个controller类添加重复的异常处理器方法显然不够简洁优雅。比较容易想到的优化方案是将异常处理器方法放在controller基类中,其他controller类通过继承该基类来获得异常处理器,但是由于java的单继承问题,会使得其无法再继承父类容易产生不便;还有一种优化方案是通过接口的默认方法实现(该特性从jdk 8开始支持),但是这样需要其他controller类都需要显式地实现该接口,稍微有点麻烦。为此spring framework提供了一个控制器通知注解——@controlleradvice

@controlleradvice注解所标识类的异常处理器方法将会对项目中所有标注了@requestmapping注解的方法生效(即项目中所有的controller方法),这样我们就可以很方便地统一处理所有controller方法所抛出的异常,同时保证了conrotller类的简洁。值得一提的是@controlleradvice注解本身已经使用了@component注解

/*** 所有controller方法统一的异常处理:controller异常处理器通知*/ @controlleradvice public class controllerexceptionhandler {/*** 异常处理器* @param e* @return*/@responsebody@exceptionhandler(exception.class)public string handleexception1(exception e) {string msg = "get exception in handleexception1 method";system.out.println("msg: " msg);return msg;}/*** 运算异常处理器* @param e* @return*/@responsebody@exceptionhandler(arithmeticexception.class)public string handleexception2(arithmeticexception e) {string msg = "get arithmeticexception in handleexception2 method";system.out.println("msg: " msg);return msg;} } ... @controller @requestmapping("student") public class studentcontroller { ...@responsebody@requestmapping("/test1")public integer test1(@requestparam int a) {int b = 10 / a;system.out.println("b: " b);return b;}... }

参考文献

  • spring实战 craig walls著、张卫滨译
  • 总结

    以上是凯发k8官方网为你收集整理的controller需要捕获异常吗_spring之controller异常处理的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

    网站地图