The @ControllerAdvice annotation is a component annotation allowing implementation classes to be auto-detected through

classpath scanning. It is automatically enabled when using the MVC namespace or the MVC Java config.

Classes annotated with @ControllerAdvice can contain @ExceptionHandler@InitBinder, and @ModelAttribute annotated

methods, and these methods will apply to @RequestMapping methods across all controller hierarchies as opposed to the controller

hierarchy within which they are declared.

The @ControllerAdvice annotation can also target a subset of controllers with its attributes:

// Target all Controllers annotated with @RestController
@ControllerAdvice(annotations = RestController.class)
public class AnnotationAdvice {} // Target all Controllers within specific packages
@ControllerAdvice("org.example.controllers")
public class BasePackageAdvice {} // Target all Controllers assignable to specific classes
@ControllerAdvice(assignableTypes = {ControllerInterface.class, AbstractController.class})
public class AssignableTypesAdvice {}
Customizing data binding with @InitBinder

Annotating controller methods with @InitBinder allows you to configure web data binding directly within your controller class. @InitBinder

identifies methods that initialize the WebDataBinder that will be used to populate command and form object arguments of annotated handler

methods.

Such init-binder methods support all arguments that @RequestMapping supports, except for command/form objects and corresponding

validation result objects. Init-binder methods must not have a return value. Thus, they are usually declared as void. Typical arguments include

WebDataBinder in combination with WebRequest orjava.util.Locale, allowing code to register context-specific editors.

The following example demonstrates the use of @InitBinder to configure a CustomDateEditor for all java.util.Date form properties.

    @InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}

Advising controllers with the @ControllerAdvice annotation

Advising controllers with the @ControllerAdvice annotation的更多相关文章

  1. Spring Web MVC框架简介

    Web MVC framework框架 Spring Web MVC框架简介 Spring MVC的核心是`DispatcherServlet`,该类作用非常多,分发请求处理,配置处理器映射,处理视图 ...

  2. SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-005- 异常处理@ResponseStatus、@ExceptionHandler、@ControllerAdvice

    No matter what happens, good or bad, the outcome of a servlet request is a servlet response. If an e ...

  3. Supported method argument types Spring MVC

    Supported method argument types The following are the supported method arguments: Request or respons ...

  4. Spring 4 官方文档学习 Web MVC 框架

    1.介绍Spring Web MVC 框架 Spring Web MVC 框架是围绕DispatcherServlet设计的,所谓DispatcherServlet就是将请求分发到handler,需要 ...

  5. Spring框架文档与API(4.3.6版本)

    http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ Table of Contents I ...

  6. Spring 4 官方文档学习(十一)Web MVC 框架

    介绍Spring Web MVC 框架 Spring Web MVC的特性 其他MVC实现的可插拔性 DispatcherServlet 在WebApplicationContext中的特殊的bean ...

  7. SpringMVC之四:渲染Web视图

    理解视图解析 在前面的例子中,我们看到控制器返回的都是一个逻辑视图的名称,然后把这个逻辑视图名称交给view resolver,然后返回渲染后的 html 页面给 client. 将控制器中请求处理的 ...

  8. 【Spring实战】----开篇(包含系列目录链接)

    [Spring实战]----开篇(包含系列目录链接) 置顶2016年11月10日 11:12:56 阅读数:3617 终于还是要对Spring进行解剖,接下来Spring实战篇系列会以应用了Sprin ...

  9. Spring学习笔记之五----Spring MVC

    Spring MVC通常的执行流程是:当一个Web请求被发送给Spring MVC Application,Dispatcher Servlet接收到这个请求,通过HandlerMapping找到Co ...

随机推荐

  1. Linux软件源

    Kali科大软件源: vim /etc/apt/sources.list 下面的粘帖进去. deb http://mirrors.ustc.edu.cn/kali kali main non-free ...

  2. WAS生成的文件:javacore.***.txt 、heapdump.***.phd、core.***.dmp、Snap.***.trc

    WAS生成的常见文件有哪些? 原文链接:http://blog.csdn.net/pqh20085101092/article/details/39370389 javacore.***.txt : ...

  3. Spark学习笔记——泰坦尼克生还预测

    package kaggle import org.apache.spark.SparkContext import org.apache.spark.SparkConf import org.apa ...

  4. 树莓派集群实践2——修改树莓派3 指定IP,DNS要修改dhcpcd.conf

    vim /etc/dhcpcd.conf 修改如下 interface eth0 static ip_address=192.168.1.177/24 static routers=192.168.1 ...

  5. last

    last reboot 显示系统最后重启的历史记录 last -w 显示自系统启动以来,所有登录的用户(全名)

  6. Kettle能做什么?

    简介 Kettle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,绿色无需安装,数据抽取高效稳定. Kettle 中文名称叫水壶,该项目的主程序员MATT  ...

  7. android bionic c 对比 gnu c

    Bionic 是一个BSD标准的C库,用在android平台上面的. Android 是一个不完全开源的系统. android的kernel使用的是基于linux的,linux使用的是GPL2的开源标 ...

  8. 一个表中的id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数

    一个表中的id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数 select id ,Count(*) from table_name group by id having count( ...

  9. [原]openstack-kilo--issue(二十一) instance can't get ip 虚拟机不能得到ip(2)

    ===问题点==== 在使用vlan模式部署compute节点的时候出现了下面的错误:在controller节点的dhcp-agent.log中 2017-01-22 20:19:34.178 241 ...

  10. spring框架排错

    1.格式错误. http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/sp ...