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中 ...
随机推荐
- phalcon无法更新分区字段解决办法
表里面有个分区字段,更新后,保存成功,结果实际上数据库没有更新. 后来使用原生sql,解决了这个问题. 获取di中的数据库连接,直接执行sql语句即可,下面的db为注册的数据库服务名 $db = $t ...
- java jar包与配置文件的写法
一个普通的java project,里面引用了config.properties配置文件,将项目打成Runnable jar,然后将config.properties放到打包后的jar路径下,执行该j ...
- Mockito 简单使用
有一个月没写博客了,以后再忙也要抽时间写啊. 目的 正常情况下,如果要对 UserService 中方法的测试,那么其依赖的 UserDao 也要可以调通,但是,UserDao 可能并不是直接到 DB ...
- 【Unity笔记】Terrain地形制作坍塌/深坑
Unity的Terrain组件在[set the terrain height]分页下,height高度为0时,可理解为该地形的海平面高度,此时就不能地形下榻.把height调到100,点击[flat ...
- ML-DL-各种资源汇总
1.Used Libraries, Datasets, and Models 1.1 Libraries TensorFlow (from Google): https://www.tensorflo ...
- MATLAB — axis
转至:http://blog.csdn.net/cs_zlg/article/details/8516463 axis中文为“轴”之意,在matlab中用于控制坐标轴的范围和样式(颜色等). axis ...
- linux 常见错误
yum.pid 已被锁定 rm -rf /var/run/yum.pid
- R语言入门 一些基本的操作
1 R的下载.安转 R有很多的版本,支持目前主流的操作系统MAC.Linux和WINDOWS系列.因为我个人是在WINDOWS下用R的,所以在这里将只介绍WINDOWS下R的下载&安装. 下载 ...
- HashMap的最大容量为什么是2的30次方?
今天看HashMap的底层实现,发现HashMap的最大容量规定为: // 最大容量(必须是2的幂且小于2的30次方,传入容量过大将被这个值替换) static final int MAXIMUM_C ...
- Loss is its own Reward: Self-Supervision for Reinforcement Learning
作者用action, reward, state等当做lalbel,进行有监督训练.