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 防火墙 ufw使用

    ufw是ubuntu是默认的防火墙配置工具,相对于iptables,ufw使用更加简单 ufw基本操作   1 []是代表可选内容,需要root权限 ufw [--dry-run] enable|di ...

  2. H3C ER6300 + 两台 H3C S5120 组网举例

    组网需求: 1.H3C ER6300 作出口路由.防火墙及Qos限速等功能(ER6300 配置LAN口 192.168.30.254默认网关) 2.H3C S5120 两台配置相同VLAN10 VLA ...

  3. PHP在linux读取word文档

    几天帮朋友解决一个技术问题,在Linux下,将word文档中的内容读取,然后使用正则匹配,拼成sql入库 查阅了外文资料和google之后,步骤如下: #wget http://www.winfiel ...

  4. Servlet知识点回顾

    一.Servlet生命周期 服务器调用一个Servlet的8个步骤: 1.在服务器启动时,当Servlet被配置好或者被客户首次请求时,由服务器加载servlet,这一步相当于下列代码: Class ...

  5. css3整理--calc()

    calc()语法: elem{ width:calc( 50% -2px ); } 取值说明: calc是计算的缩写,上句含义是:计算elem父元素的50%再减2px,并将结果赋值给elem的widt ...

  6. 解决在html中引入font-awesome的css文件后, 图标显示不出来

    今天小颖在做项目时,需要在html文件中引入font-awesome.min.css,但是引入后: 以前小颖在用font-awesome库里的图标时,都是直接从node中下包,然后在main.js中引 ...

  7. [原]openstack-kilo--issue(十九) ImportError: Could not import settings 'openstack_dashboard.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named main

    查看此问题的时候请先查看 这个问题包含在(十)中,此篇只是从(十)中分离出来 openstack-kilo--issue(十)ERROR: openstack Unable to establish ...

  8. 拓展 NLog 优雅的输送日志到 Logstash

    在上上篇博客通过对aspnetcore启动前配置做了一些更改,以及对nlog进行了自定义字段,可以把请求记录输送到mysql,正式情况可能不会这么部署.因为近期也在学习elk,所以就打算做一个实例,结 ...

  9. 微信生成二维码 只需一个网址即刻 还有jquery生成二维码

    <div class="orderDetails-info"> <img src="http://qr.topscan.com/api.php?text ...

  10. PHP数组(数组正则表达式、数组、预定义数组)

    正则表达式 1.替换 $s = "hello5world"; $s = preg_replace("/\d/","#",$s); echo ...