在Spring3.2中,新增了@ControllerAdvice注解。关于这个注解的官方说明https://docs.spring.io/spring-framework/docs/5.0.0.M1/javadoc-api/org/springframework/web/bind/annotation/ControllerAdvice.html。

我们可以根据字面意思将这个注解理解为Controller的Advice(在spring aop中,Advice被翻译为“增强”)。

在这个控制器的增强处理中,spring提供了三个注解,来帮助我们对Controller进行增强的操作:

@ExceptionHandler:异常处理器、@InitBinder:初始参数绑定器、@ModelAttribute(模型参数)。

@ControllerAdvice

先来看下@ControllerAdvice的内容,这个注解是使用在Class上,里面有几个参数可以指定basePackages,但由于这个注解只是对Controller的增强,因此只对指定包下的Controller生效(如果在dao或service包中对异常上抛,也是可以抛出到Controller层,并通过搭配该注解进行捕获异常)。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice {
@AliasFor("basePackages")
String[] value() default {}; @AliasFor("value")
String[] basePackages() default {}; Class<?>[] basePackageClasses() default {}; Class<?>[] assignableTypes() default {}; Class<? extends Annotation>[] annotations() default {};
}

@ExceptionHandler

@ExceptionHandler:这个注解是使用在方法上,可以处理指定的异常。假如异常不在这个范围内,则不会被捕获,可以定义多个不同的异常处理器用来分别处理不同的异常。

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExceptionHandler {
Class<? extends Throwable>[] value() default {};
}

我们来测试一下@ExceptionHandler

定义一个全局异常处理器,里面指定了两个异常的捕获和处理,一个是IAE,一个是NPE。

@ControllerAdvice
public class GlobalExceptionHandler { @ExceptionHandler(value = IllegalArgumentException.class) //指定异常只处理IAE,也可以指定为Throwable类,可以处理所有的异常。
@ResponseBody //这里添加ResponseBody是为了直接返回字符串到前端,否则会如同Controller一样,将返回的字符串作为视图名处理,向视图发起请求。
public String handler(Exception exception) {
System.out.println("抓住异常,异常原因:");
System.out.println(exception.getMessage());
return "IllegalArgumentException";
} @ExceptionHandler(value = NullPointerException.class)
@ResponseBody
public String handler2(Exception exception) {
System.out.println("抓住异常,异常原因:");
System.out.println(exception.getMessage());
return "NullPointerException";
} }

然后我们定义一个Controller,里面会抛出上面的两种异常

    @GetMapping("users/{id}")
public String getUser(@PathVariable("id") String id) throws Exception {
System.out.println("接收到请求[/users/" + id + "]");
if (id.equals("error")) {
throw new IllegalArgumentException("参数错误!");
} else if (id.equals("null")) {
throw new NullPointerException("id不能为null!");
}
return "testUser";
}

启动项目,分别输入参数为error和null的请求,查看控制台打印数据:

接收到请求[/users/error]
抓住异常,异常原因:
参数错误! 接收到请求[/users/error]
抓住异常,异常原因:
id不能为null!

如果我们在controller里面再抛出一个其他类型的异常,则会直接返回到前端,而不是被交给异常处理器处理。

@InitBinder

这个注解用来对从前端传入的参数进行功能辅助。

具体的辅助功能有很多种。比如我们传了一个String类型的"2019-05-05",但是我们在controller要用Date来获取,默认情况下是会抛出非法参数异常,如下:

因此这个时候需要我们在@InitBinder中配置一个自定义日期格式,如下:

    @InitBinder
public void globalInitBinder(WebDataBinder binder) {
binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
}

这个注解关键的重点在于它的参数WebDataBinder中配置的信息,除了上面的添加自定义格式外,还可以注册其他的类型,如将参数自动存储在Map中。

    @InitBinder
public void globalInitBinder(WebDataBinder binder) {
binder.registerCustomEditor(Integer.class, new CustomMapEditor(HashMap.class));
}

@ModelAttribute

该注解用来在Controller接收参数之前对数据模型进行处理,可以添加或删除相关的数据,比如我们在@ModelAttrbute中配置两个数据name和age

    @ModelAttribute
public void model(Model model) {
model.addAttribute("name", "yxf");
model.addAttribute("age", );
}

然后在Controller的方法中进行配置

    @GetMapping("users/login")
public String test(@ModelAttribute("name")String name, @ModelAttribute("age") int age) {
System.out.println("Name="+name + ";Age="+age);
return "index";
}

配置完成后运行启动类,输入路径"/users/login"不带任何参数。

控制台照样会打印结果:Name=yxf;Age=12

Spring boot--控制器增强的更多相关文章

  1. springboot2.0(一):【重磅】Spring Boot 2.0权威发布

    就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...

  2. 业余草分享 Spring Boot 2.0 正式发布的新特性

    就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...

  3. 【重磅】Spring Boot 2.0权威发布

    新版本特性 新版本值得关注的亮点有哪些: 基于 Java 8,支持 Java 9 也就是说Spring Boot2.0的最低版本要求为JDK8,据了解国内大部分的互联网公司系统都还跑在JDK1.6/7 ...

  4. (转)Spring Boot 2(一):【重磅】Spring Boot 2.0权威发布

    http://www.ityouknow.com/springboot/2018/03/01/spring-boot-2.0.html 就在今天Spring Boot2.0.0.RELEASE正式发布 ...

  5. Spring Boot 2(一):Spring Boot 2.0新特性

    Spring Boot 2(一):Spring Boot 2.0新特性 Spring Boot依赖于Spring,而Spring Cloud又依赖于Spring Boot,因此Spring Boot2 ...

  6. spring boot 2.0(一)权威发布spring boot2.0

    Spring Boot2.0.0.RELEASE正式发布,在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring ...

  7. Spring Boot 2(一):【重磅】Spring Boot 2.0权威发布

    就在今天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...

  8. Spring Boot 非常好的学习资料

    from@https://gitee.com/didispace/SpringBoot-Learning Spring Boot 2.0 新特性学习 简介与概览 Spring Boot 2.0 正式发 ...

  9. spring boot学习01【搭建环境、创建第一个spring boot项目】

    1.给eclipse安装spring boot插件 Eclipse中安装Spring工具套件(STS): Help -> Eclipse Marketplace... 在Search标签或者Po ...

  10. Spring Boot (八): Mybatis 增强工具 MyBatis-Plus

    1. 简介 在上一篇文章<Spring Boot (七): Mybatis极简配置> 中我们介绍了在 Spring Boot 中 Mybatis 的基础使用方式,其中有一部分美中不足的是 ...

随机推荐

  1. Django项目:CRM(客户关系管理系统)--71--61PerfectCRM实现CRM学生上传作业

    # student_urls.py # ————————60PerfectCRM实现CRM学生上课记录———————— from django.conf.urls import url from bp ...

  2. (转)AngularJS中使用的表单验证

    原文  http://www.cnblogs.com/woshinidezhu/p/Form-validation-with-AngularJS.html 客户端表单验证是AngularJS里面最酷的 ...

  3. 线段树区间更新+区间求和模板(数组实现)洛谷p3372,p3373

    模板题目1:https://www.luogu.org/problemnew/show/P3372 懒惰标记讲解:https://www.cnblogs.com/wushengyang/p/11194 ...

  4. PAT甲级——A1029 Median

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  5. C++ Primer 阅读小结

    一.变量和基本类型 类型是C++程序设计的基础. 每种类型都定义了其存储空间要求和可以在该类型的所有对象上执行的操作.C++提供了一组基本内置类型,如int.char等.这些类型与它们在机器硬件上的标 ...

  6. python实现简单的百度翻译

    这段时间,一直在学python,想找点东西实现一下,练手,所以我想通过python代码来实现翻译,话不多说,看吧! 以chrome为例 1  打开百度翻译 https://fanyi.baidu.co ...

  7. Django REST Framework概述

    什么是REST REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移”.这里说的表征性,就是指资源,通 ...

  8. hive-hbase性能问题

    华为负责人本来想用这种表来做大数据开发,先前就听前辈讲过性能存在问题.实际开发过程确实存在不少问题.然后放弃换方案去做了. 1.底层meta映射字段问题.默认4000,如果再做修改会涉及到挺多源码. ...

  9. react-native warn Failed to connect to development server using "adb reverse":

    react-native环境搭建中的问题 本文环境说明:开发环境:window10专业版,目标平台:Android react: , react-native: java: 1.8.0_221 pyt ...

  10. Go之路之go语言结构

    Go Hello World 实例 package main //定义了包名,必须在源文件中非注释的第一行指名这个文件属于哪个包,每个Go应用程序都包含一个名为main的包 import " ...