springboot使用@ControllerAdvice(二)之深入理解
前言:
1. ModelAttribute: 暴露@RequestMapping 方法返回值为模型数据:放在功能处理方法的返回值上时,是暴露功能处理方法的返回值为模型数据,用于视图页面展示时使用。
2. InitBinder : 用于自定义@RequestMapping 方法参数绑定
3. ResponseBodyAdvice : 用于@ResponseBody返回值增加处理
ResponseBodyAdvice
ResponseBodyAdvice是spring4.1的新特性,其作用是在响应体写出之前做一些处理;比如,修改返回值、加密等。
我在项目中的用到@ControllerAdvice,ResponseBodyAdvice来做一个Controller增强器,将Controller接口的响应(Response)放到请求上下文中 。
介绍:https://blog.csdn.net/u012852374/article/details/53837156
项目中Controller增强器:

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; import com.generalichina.wolong.base.BaseResponse; @ControllerAdvice
public class ResponseAdvisor implements ResponseBodyAdvice<Object> { /**
* 判断支持的类型
*/
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
//注意,这里必须返回true,否则不会执行beforeBodyWrite方法
return true;
} /**
* 过滤
*
*/
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) { String requestPath = request.getURI().getPath();// 普通response不进行过滤
if (!requestPath.startsWith("/credit")) {
return body;
} //如果是返回客户端模板类不进行过滤
if (body instanceof BaseResponse) {
return body;
} return new BaseResponse(body);
} }
RequestBodyAdvice和ResponseBodyAdvice
springboot使用@ControllerAdvice(二)之深入理解的更多相关文章
- Kotlin开发springboot项目(二)
Kotlin开发springboot项目(二) 中文学习网站: https://www.kotlincn.net/ 研究一下kotlin的demo: https://github.com/JetBra ...
- SpringBoot第十二集:度量指标监控与异步调用(2020最新最易懂)
SpringBoot第十二集:度量指标监控与异步调用(2020最新最易懂) Spring Boot Actuator是spring boot项目一个监控模块,提供了很多原生的端点,包含了对应用系统的自 ...
- SpringBoot进阶教程(二十九)整合Redis 发布订阅
SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...
- SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1
在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...
- SpringBoot入门(二)——起步依赖
本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...
- SpringBoot 基础(二)
目录 SpringBoot基础(二) 一.操作数据库 1. SpringBootJdbc 2. SpringBoot 整合 Mybatis 3. SpringBott 使用JPA 二.使用 Thyme ...
- SpringBoot系列(二)入门知识
SpringBoot系列(二)入门知识 往期推荐 SpringBoot系列(一)idea新建springboot项目 引言 本来新建springboot项目应该放在入门知识这一章的,但是由于新建spr ...
- SpringBoot系列(十二)过滤器配置详解
SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...
- 十九、springboot使用@ControllerAdvice(二)之深入理解
前言: 接口类项目开发时,为了便于后期查找问题,一般会拦截器或过滤器中记录每个接口请求的参数与响应值记录, 请求参数很容易从request中获取,但controller的返回值无法从response中 ...
随机推荐
- 纯CSS3打造非常炫的加载动画
纯css3打造的一款非常炫的加载图.用在需要一定时间加载的地方非常合适.先上效果图: 点击这里在线预览 代码非常简单.没有用任何javascript代码.纯css3实现. html代码: <di ...
- JS自动关闭授权弹窗,并刷新父页面
echo "<script>window.opener.location.href='index.php'; window.close();</script>&quo ...
- 【Java】使用pinyin4j获取汉字的全拼或首字母
汉字转拼音的工具类,常用于做汉字拼音的模糊查询. https://www.cnblogs.com/htyj/p/7891918.html
- C语言 · 陶陶摘苹果2
算法提高 陶陶摘苹果2 时间限制:1.0s 内存限制:256.0MB 问题描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出n个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶有个 ...
- 采用dlopen、dlsym、dlclose dlopen dlerror加载动态链接库【总结】
.前言 为了使程序方便扩展,具备通用性,可以采用插件形式.采用异步事件驱动模型,保证主程序逻辑不变,将各个业务已动态链接库的形式加载进来,这就是所谓的插件.linux提供了加载和处理动态链接库的系统调 ...
- Android——事务
一.什么是事务 事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消.也就是事务具有原子性,一个事务中的一系列的操作要么全部成功,要么一个都不做. 事务的结 ...
- sqoop 从sqlserver2008 导入数据到hadoop
今天终于开始上手导入数据到hadoop了,哈哈,过程蛮崎岖的,和官方文档的还不太一样. OK,let's go!试验对象是我第一个名为ST_Statistics的一张表,我要把我表里的数据导入到hdf ...
- 【oneday_onepage】——Microsoft adds a wing, more closets to the homes of SharePoint Online tenants
To prevent SharePoint Online customers from feeling boxed in, Microsoft wants to improve the way the ...
- 【oneday_onepage】——China's Internet users grow to 591 million
July 17, 2013, 6:29 AM — China's Internet populace grew to 591 million by the end of June, as more n ...
- Navi.Soft31.Mobile框架(含下载地址)
1概述 1.1应用场景 互联网的发展,使用基于Web的软件异军突起,目前占据着相当大的市场份额,而手机,平板电脑等移动端设备的频繁使用,使移动端的软件快速发展,逐步有超越Web软件的趋势 移动软件中, ...