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对象与map对象相互转换
/** * 使用org.apache.commons.beanutils进行转换 */ class A { public static Object mapToObject(Map<String ...
- Writing DynamicTableEntity to Azure Storage Table
There are ample of samples available to show how to insert an object/entity to Azure Storage Table. ...
- 30分钟入门Java8之lambda表达式
前言 Google在今年发布Android N开发者预览版,一并宣布开始支持Java 8.我们终于能在Android开发中使用到Java8的一些语言特性了.目前支持: 默认方法 lambda表达式 多 ...
- linux环境中,多个命令之间,通过&& ||执行,命令之间执行的逻辑是什么?
需求描述: 最近在写一个脚本的时候,要判断一个文件是否存在,有怎么样,没有就创建,这个时候 看到了一个test 结合 || 或者 &&的写法,就查看了下资料记录下是怎么个玩法. 操作过 ...
- [Converge] Feature Selection in training of Deep Learning
特征相关性对于DL的影响 链接:https://www.zhihu.com/question/47908908/answer/110987483 经验一: 1. 输入特征最好不相关.如果某些维输入的 ...
- lua_tinker源码笔记1
本篇仅简单介绍使用lua_tinker让lua调用C++函数的过程, C++调用lua函数可以参见博客Lua脚本和C++交互(一). 未完待续. (一) lua调用C++全局函数 这里以lua_tin ...
- 在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作。一次移动操作指用一个"LX"替换一个"XL",或者用一个"XR"替换一个"RX"。现给定起始字符串start和结束字符串end,请编写代码,当且仅当存在一系列移动操作使得start可以转换成end时, 返回True。
在一个由 'L' , 'R' 和 'X' 三个字符组成的字符串(例如"RXXLRXRXL")中进行移动操作.一次移动操作指用一个"LX"替换一个"XL ...
- css3整理--background-image
background-image语法: background-image: url1,url2,...,urlN; 通过“,”分隔N张背景图片,background的所有其它属性需要配合该属性进行设置 ...
- 【CF944G】Coins Exhibition DP+队列
[CF944G]Coins Exhibition 题意:Jack去年参加了一个珍稀硬币的展览会.Jack记得一共有 $k$ 枚硬币,这些硬币排成一行,从左到右标号为 $1$ 到 $k$ ,每枚硬币是正 ...
- Jenkins解决无法获取插件的办法(升级站点目录)
Jenkins解决无法获取插件的办法 可能是由于Jenkins的更新网站被QIANG,因此,请替换插件的服务器地址: http://mirror.xmission.com/jenkins/update ...