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 ...
随机推荐
- 理解Java枚举类型
(参考资料:深入理解java enum) 1.原理:对编译后的class文件javap反编译可以看出,定义的枚举类继承自java.lang.Enum抽象类且通过public static final定 ...
- Generate class from database table How can i generate a class from a table at a SQL Server?
Set @TableName to the name of your table. declare @TableName sysname = 'TableName' declare @Result v ...
- 仿迅雷播放器教程 -- C++ 100款开源界面库 (10)
(声明:Alberl以后说到开源库,一般都是指著名的.或者不著名但维护至少3年以上的.那些把代码一扔就没下文的,Alberl不称之为开源库,只称为开源代码.这里并不是贬低,像Alberl前面那个系 ...
- 自己开发chrome插件生成二维码
摘要: 最近在开发微信项目时,需要在微信调试,所以经常会在微信中输入本地服务地址,输入起来特别麻烦,所以自己就想了想微信中的扫一扫,然后开发了这款chrome插件,将当前url生成二维码,用微信扫一扫 ...
- 【algorithm】 二分查找算法
二分查找算法:<维基百科> 在计算机科学中,二分搜索(英语:binary search),也称折半搜索(英语:half-interval search)[1].对数搜索(英语:logari ...
- Web上传文件的原理及实现
现在有很多Web程序都有上传功能,实现上传功能的组件或框架也很多,如基于java的Commons FileUpload.还有Struts1.x和Struts2中带的上传文件功能(实际上,Struts2 ...
- Using Java in Debian
Using Java in Debian 要安装个solr的环境调试,需要安装java,我的虚拟机使用的是debian,搜索了好多还是sun时代的写的blog.最后在debian的官网找到如下: ht ...
- Mac下利用Cordova打包 iOS App以及出现的问题
安装 cordova sudo npm install cordova 创建项目 创建一个demo文件夹,里面自动加载基本的文件以及目录 cordova create demo com.test.de ...
- c++中map按key和value排序
``` #include<iostream> #include<algorithm> #include<vector> #include<map> #i ...
- db2 基础语法
一.db2 基础 基本语法 注释:“--”(两个减号) 字符串连接:“||” 如set msg=’aaaa’||’bbbb’,则msg为’aaaabbbb’ 字符串的引用:‘’(一定用单引号),如果需 ...