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. hello alibaba

    http://ifeve.com/dubbo-learn-book/ http://ifeve.com/leader-follower-thread-model/ http://ifeve.com/a ...

  2. css背景色半透明的最佳实践

    之前项目中遇到纯色的半透明背景,都是这么干: <style> .box {width:300px;height:300px;position:relative;} .mask {width ...

  3. react-router 4.3 js实现跳转

    import React, {Component} from 'react'; import { NavLink,Link } from "react-router-dom"; i ...

  4. 分辨率,PPi,DPI,DPR,物理像素,逻辑像素

    屏幕尺寸:指的是屏幕对角线的长度 分辨率:是指宽度上和高度上最多能显示的物理像素点个数 点距:像素与像素之间的距离,点距和屏幕尺寸决定了分辨率大小 PPI:屏幕像素密度,即每英寸(1英寸=2.54厘米 ...

  5. 解决pycharm在ubuntu下搜狗输入法一直固定在左下角的问题

    1.缩放VMware,当ubuntu中出现下拉导航条时,点击左上角查看>立即适应客户机,然后在pycharm中打中文的时候不用全屏,就可以看到输入法显示的文字了. 2.目前没有发现搜狗输入法版本 ...

  6. 揭秘IT人才特点:中美印日四国程序员比较

    不知道大家是否已经看过这篇文章,感觉比较客观.   来自:http://www.programmer.com.cn/282/        最近以裁判的身份参加了公司举办的编程大赛,发现高手云集,对公 ...

  7. windows 清理利器

    https://www.ccleaner.com/ 2. https://www.chuyu.me/zh-Hans/index.html

  8. 【react】---pureComponent的理解

    一.pureComponent的理解  pureComponent表示一个纯组件,可以用来优化react程序.减少render函数渲染的次数.提高性能 pureComponent进行的是浅比较,也就是 ...

  9. Python2.7编译失败 Failed to build these modules:_curses_panel _hashlib _ssl

    1:*** WARNING: renaming "_ssl" since importing it failed: libssl.so.1.0.0: cannot open sha ...

  10. Linux 创建文件系统及挂载文件系统流程详解(转)

    作者:北南南北 来自: LinuxSir.Org 摘要:本文对新增硬盘,切割硬盘,创建硬盘分区,为硬盘分区创建文件系统,以及加载文件系统的流程做总结性论述:主要是为初学者弄清楚这一操作过程:本文涉及f ...