• 瑞吉外卖(五) 全局异常处理


    如何进行全局异常处理?

    请添加图片描述
    请添加图片描述
    请添加图片描述

    效果展示

    在这里插入图片描述

    @ControllerAdvice

    在这里插入图片描述
    本质上就是Component,然后,我们来看一下此类的注释:

    这个类是为那些声明了(@ExceptionHandler、@InitBinder 或 @ModelAttribute注解修饰的)方法的类而提供的专业化的@Component , 以供多个 Controller类所共享。

    说白了,就是aop思想的一种实现,你告诉我需要拦截规则,我帮你把他们拦下来,具体你想做更细致的拦截筛选和拦截之后的处理,你自己通过@ExceptionHandler、@InitBinder 或 @ModelAttribute这三个注解以及被其注解的方法来自定义。

    • @ExceptionHandler
      在这里插入图片描述
      接收Throwable类作为参数,我们知道Throwable是所有异常的父类,所以说,可以自行指定所有异常

    比如在方法上加:@ExceptionHandler(IllegalArgumentException.class),则表明此方法处理
    IllegalArgumentException 类型的异常,如果参数为空,将默认为方法参数列表中列出的任何异常(方法抛出什么异常都接得住)。

    下面的例子:处理所有IllegalArgumentException异常,域中加入错误信息errorMessage 并返回错误页面error

    • @ControllerAdvice 配合 @ModelAttribute 预设全局数据
    /**
     * Annotation that binds a method parameter or method return value
     * to a named model attribute, exposed to a web view. Supported
     * for controller classes with {@link RequestMapping @RequestMapping}
     * methods.
     * 此注解用于绑定一个方法参数或者返回值到一个被命名的model属性中,暴露给web视图。支持在
     * 在Controller类中注有@RequestMapping的方法使用(这里有点拗口,不过结合下面的使用介绍
     * 你就会明白的)
     */
    @Target({ElementType.PARAMETER, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface ModelAttribute {
    
    	@AliasFor("name")
    	String value() default "";
    
    	@AliasFor("value")
    	String name() default "";
    
    	boolean binding() default true;
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    实际上这个注解的作用就是,允许你往 Model 中注入全局属性(可以供所有Controller中注有@Request Mapping的方法使用),value 和 name 用于指定 属性的 key ,binding 表示是否绑定,默认为 true。

    具体使用方法如下:
    在这里插入图片描述

    • @InitBinder

    @ControllerAdvice 配合 @InitBinder 实现对请求参数的预处理

    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface InitBinder {
    	/**
    	 * The names of command/form attributes and/or request parameters
    	 * that this init-binder method is supposed to apply to.
    	 * 

    Default is to apply to all command/form attributes and all request parameters * processed by the annotated handler class. Specifying model attribute names or * request parameter names here restricts the init-binder method to those specific * attributes/parameters, with different init-binder methods typically applying to * different groups of attributes or parameters. * 粗略翻译:此init-binder方法应该应用于的命令/表单属性和/或请求参数的名称。默认是应用于所有命 * 令/表单属性和所有由带注释的处理类处理的请求参数。这里指定模型属性名或请求参数名将init-binder * 方法限制为那些特定的属性/参数,不同的init-binder方法通常应用于不同的属性或参数组。 * 我至己都理解不太理解这说的是啥呀,我们还是看例子吧 */ String[] value() default {}; }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    这样之后呢,就可以实现全局的实现对 Controller 中RequestMapping标识的方法中的所有 String 和Date类型的参数都会被作相应的处理。

    在这里插入图片描述

  • 相关阅读:
    如何用多因素认证(MFA)保护 Windows RDP 端口?
    关于比较两个对象属性
    js多级嵌套选中效果代码实现-vue
    Android开发版本和API等级对应关系
    5个免费样机素材网站,设计必备,赶紧马住!
    变电站远程维护解决方案,工程师从此不再“跑断腿“
    实现动态页面的技术Servlet
    vue小技能:使用渲染函数编写组件
    linux 压缩命令
    SpringMVC基于注解使用:异常处理
  • 原文地址:https://blog.csdn.net/futurn_hero/article/details/128087709