Advising controllers with the @ControllerAdvice annotation
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的更多相关文章
- Spring Web MVC框架简介
Web MVC framework框架 Spring Web MVC框架简介 Spring MVC的核心是`DispatcherServlet`,该类作用非常多,分发请求处理,配置处理器映射,处理视图 ...
- 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 ...
- Supported method argument types Spring MVC
Supported method argument types The following are the supported method arguments: Request or respons ...
- Spring 4 官方文档学习 Web MVC 框架
1.介绍Spring Web MVC 框架 Spring Web MVC 框架是围绕DispatcherServlet设计的,所谓DispatcherServlet就是将请求分发到handler,需要 ...
- Spring框架文档与API(4.3.6版本)
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ Table of Contents I ...
- Spring 4 官方文档学习(十一)Web MVC 框架
介绍Spring Web MVC 框架 Spring Web MVC的特性 其他MVC实现的可插拔性 DispatcherServlet 在WebApplicationContext中的特殊的bean ...
- SpringMVC之四:渲染Web视图
理解视图解析 在前面的例子中,我们看到控制器返回的都是一个逻辑视图的名称,然后把这个逻辑视图名称交给view resolver,然后返回渲染后的 html 页面给 client. 将控制器中请求处理的 ...
- 【Spring实战】----开篇(包含系列目录链接)
[Spring实战]----开篇(包含系列目录链接) 置顶2016年11月10日 11:12:56 阅读数:3617 终于还是要对Spring进行解剖,接下来Spring实战篇系列会以应用了Sprin ...
- Spring学习笔记之五----Spring MVC
Spring MVC通常的执行流程是:当一个Web请求被发送给Spring MVC Application,Dispatcher Servlet接收到这个请求,通过HandlerMapping找到Co ...
随机推荐
- javascript实现一行文字随不同设备自适应改变字体大小至字数完全展示
产品提了一个小需求,希望一行能展示用户输入的所有文字,因为最多限制为25字符,但是如果夹杂英文/韩文/日文等,即使字符数是一样的,但是展示的长度不一样,则有些title标题会被截断. 效果如图 前提是 ...
- [algorithm] 汉诺塔问题
汉诺塔是根据一个传说形成的一个问题.汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘.大梵天命令婆罗 ...
- [Bayes] KL Divergence & Evidence Lower Bound
L lower是什么? L lower, 既然大于,那么多出来的这部分是什么?如下推导: 得出了KL的概念,同时也自然地引出了latent variable q.
- "佛祖保佑 永无bug" 注释模板设置详解(仅供娱乐)
1.注释模板效果图 今天在网上看到一段有趣的注释,佛祖保佑 永无bug, 效果如下图所示: 代码如下所示: /** * _ooOoo_ * o8888888o * 88" . " ...
- web java -- 连接池 -- 概述
1. 连接池的实现原理 1. 创建连接池 首先要创建一个静态的连接池.这里的“静态”是指池中的连接时在系统初始化时就分配好的,并且不能够随意关闭.Java 提供了很多容器类可用来构建连接池,例如Vec ...
- 中文数据解码报错 UnicodeDecodeError: 'gbk' codec can't decode bytes in position 2-3: illegal multibyte sequence
UnicodeDecodeError: 'gbk' codec can't decode bytes in position 2-3: illegal multibyte sequence 失败原因: ...
- Intelij的idea和pycharm的使用
idea是一个很复杂但是要页很好使用的工具,其集成了很多功能,也觉定了其很多的快捷键,要在使用得过程中总结其好处. 1.psvm:public static void main(){ } 2 ...
- mysql学习之索引
首先,看一个例子,有一张大表,记录数超过1000,SELECT * FROM student WHERE name='xinan'; 如果没有索引,查找程序就得从头查找,很费时间,表越大越费时间.建立 ...
- 6.13 py网络编程
tcp的十种状态 python使用原始套接字才可以做黑客攻击!!!!!伪造数据包!!!!!直接越过底层!socket这个东西是经过linux过滤过得! TTL 每经过一个路由器就减1 谁先调clos ...
- Sqlite数据库管理(SQLite Developer) v4.0.0.528 中文版+破解方法(申明:来源于网络)
Sqlite数据库管理(SQLite Developer) v4.0.0.528 中文破解版 SharpPlus Sqlite Developer 特性: -强大的SQL编辑器 *Sqlite Sql ...