SpringMVC的@ControllerAdvice注解

本文介绍了SpringMVC的@ControllerAdvice注解,主要用于全局异常处理和数据绑定。通过@ControllerAdvice注解的类可以被自动扫描到,@ExceptionHandler用于处理异常,@ModelAttribute则用于实现全局数据绑定。此外,还提到了官方文档的相关内容,包括DataBinder的职责、异常处理以及ControllerAdvice的适用范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接:https://blog.wanvale.com/archives/97/

概述

@ControllerAdvice
刚接触SpringMVC应该很少会见到这个注解,其实它的作用非常大。
这里来简单介绍一下,如果需要更详细的介绍,建议查询springmvc官方文档,里面的介绍非常详细。
@ControllerAdvice是SpringMVC从3.2开始提供的注解

org.springframework.web.bind.annotation.ControllerAdvice

用它标注的Class能被自动扫描到,主要是用来辅助Controller的
ControllerAdvice主要作用如下:

全局异常处理

定义一个类,用ControllerAdvice注解

// 全局异常处理
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public String myHandler(Model model,Exception e) {
        model.addAttribute("message", e.getMessage());
        return "errorPage";
    }
}

在自定义的类中可以定义多个方法,不同方法处理不同异常,也可以用一个方法处理所有异常。
@ExceptionHandler注解用于指定要处理的异常类型。

全局数据绑定

@ControllerAdvice
public class GlobalExceptionHandler {
    //定义全局数据
    @ModelAttribute(name="globalData")
    public String setGlobalData(){
        return "globalDataString";
    }
}

这样在任意一个Controller里面就都可以获取到该变量了

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(ModelMap modelMap) throws Exception {
        System.out.println(modelMap.get("globalData"));
        return "hello";
    }
}

注:@ModelAttribute
使用该注解标记的方法返回的数据就是一个全局数据。默认情况下key是返回的变量名,value是方法的返回值。我们可以用注解的name属性指定一个key。

附 官方文档相关内容

Spring Framework 5.2.5 Release Docs

1.1.7 Table2 Line5

HandlerExceptionResolverDescription
ExceptionHandlerExceptionResolverResolves exceptions by invoking an @ExceptionHandler method in a @Controller or a @ControllerAdvice class. See @ExceptionHandler methods.

1.3.5 DataBinder

@Controller or @ControllerAdvice classes can have @InitBinder methods that initialize instances of WebDataBinder, and those, in turn, can:

  • Bind request parameters (that is, form or query data) to a model object.
  • Convert String-based request values (such as request parameters, path variables, headers, cookies, and others) to the target type of controller method arguments.
  • Format model object values as String values when rendering HTML forms.

1.3.6 Exceptions

@Controller and @ControllerAdvice classes can have @ExceptionHandler methods to handle exceptions from controller methods

1.3.7 ControllerAdvice

Typically @ExceptionHandler, @InitBinder, and @ModelAttribute methods apply within the @Controller class (or class hierarchy) in which they are declared. If you want such methods to apply more globally (across controllers), you can declare them in a class annotated with @ControllerAdvice or @RestControllerAdvice.

1.4.5 Filtering Handler Functions

You can filter handler functions by using the before, after, or filter methods on the routing function builder. With annotations, you can achieve similar functionality by using @ControllerAdvice, a ServletFilter, or both. The filter will apply to all routes that are built by the builder. This means that filters defined in nested routes do not apply to “top-level” routes.

小结

文档是个好东西,就是看起来头有点大#(喜极而泣)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值