前言:

接口类项目开发时,为了便于后期查找问题,一般会拦截器或过滤器中记录每个接口请求的参数与响应值记录,
请求参数很容易从request中获取,但controller的返回值无法从response中获取,有一个简单的方法,在controller接口的最后将返回值保存到request域中,这种方法虽然简单,但是开发起来太麻烦,需要在每个controller的最后添加一行代码,且该功能不属于业务功能,不应该接口中去实现,应该有个全局的处理方法。
 
ControllerAdvice是springmvc controller增强器
 
ControllerAdvice三个用处:
1. ModelAttribute: 暴露@RequestMapping 方法返回值为模型数据:放在功能处理方法的返回值上时,是暴露功能处理方法的返回值为模型数据,用于视图页面展示时使用。
2. InitBinder : 用于自定义@RequestMapping 方法参数绑定
3. ResponseBodyAdvice : 用于@ResponseBody返回值增加处理
ControllerAdvice初始化:
Spring mvc 启动时调用RequestMappingHandlerAdapter类的initControllerAdviceCache()方法进行初始化
 
ResponseBodyAdvice : 可以对@ResponseBody的返回值进行加工处理,它是一个接口类,具体处理可以自定义实现类注入到responseBodyAdviceBeans中既可,注入过程由RequestMappingHandlerAdapter类的initControllerAdviceCache去做,开发者只需要自定义实现类实现ResponseBodyAdvice 接口并添加类注解@ControllerAdvice
 

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(二)之深入理解的更多相关文章

  1. Kotlin开发springboot项目(二)

    Kotlin开发springboot项目(二) 中文学习网站: https://www.kotlincn.net/ 研究一下kotlin的demo: https://github.com/JetBra ...

  2. SpringBoot第十二集:度量指标监控与异步调用(2020最新最易懂)

    SpringBoot第十二集:度量指标监控与异步调用(2020最新最易懂) Spring Boot Actuator是spring boot项目一个监控模块,提供了很多原生的端点,包含了对应用系统的自 ...

  3. SpringBoot进阶教程(二十九)整合Redis 发布订阅

    SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...

  4. SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1

    在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...

  5. SpringBoot入门(二)——起步依赖

    本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...

  6. SpringBoot 基础(二)

    目录 SpringBoot基础(二) 一.操作数据库 1. SpringBootJdbc 2. SpringBoot 整合 Mybatis 3. SpringBott 使用JPA 二.使用 Thyme ...

  7. SpringBoot系列(二)入门知识

    SpringBoot系列(二)入门知识 往期推荐 SpringBoot系列(一)idea新建springboot项目 引言 本来新建springboot项目应该放在入门知识这一章的,但是由于新建spr ...

  8. SpringBoot系列(十二)过滤器配置详解

    SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...

  9. 十九、springboot使用@ControllerAdvice(二)之深入理解

    前言: 接口类项目开发时,为了便于后期查找问题,一般会拦截器或过滤器中记录每个接口请求的参数与响应值记录, 请求参数很容易从request中获取,但controller的返回值无法从response中 ...

随机推荐

  1. 建立window SVN服务器

    在windows下搭建SVN服务器: 首先从http://www.visualsvn.com/server/download/ 下载最新的VisualSVN-Server-x.x.x.msi,然后本机 ...

  2. Swift 编程思想 阅读笔记

    Swift 编程思想,第一部分:拯救小马html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeM ...

  3. 【微信小程序】scroll-view与Page下拉冲突

    需求:主界面是个列表.列表可以纵向滑动,下拉添加新的条目Item.每个条目Item可以横向滑动. 发现做下拉时,用Page的enablePullDownRefresh和scroll-view条目的横向 ...

  4. Redis有序集合

    Redis有序集合类似Redis集合存储在设定值唯一性.不同的是,一个有序集合的每个成员带有分数,用于以便采取有序set命令,从最小的到最大的分数有关. Redis 有序set添加,删除和测试中的O( ...

  5. SSM Spring +SpringMVC+Mybatis 整合配置 及pom.xml

    SSM Spring +SpringMVC+Mybatis 配置 及pom.xml SSM框架(spring+springMVC+Mybatis) pom.xml文件 maven下的ssm整合配置步骤

  6. 解决linux下不生成core dump文件

    core dump的概念: A core dump is the recorded state of the working memory of a computer program at a spe ...

  7. 想做web开发 就学JavaScript

    有一天我被问到,为了快速地在 web 开发工作上增加优势,应该学习什么语言.我的思绪回到了大学,那时候我用 Pascal.Fortran.C和汇编语言,不过那个时候有不同的目标. 鉴于当前的状况和趋势 ...

  8. android 图片处理

    一.缩放 指宽.高缩放 (1)按比例缩放 在开发图片浏览器等软件是,很多时候要显示图片的缩略图,而一般情况下,我们要将图片按照固定大小取缩略图,一般取缩略图的方法是使用BitmapFactory的de ...

  9. Self_Java + Selenium + Maven 环境搭建步骤

    转自:http://www.jianshu.com/p/3c05e8c9ee81 我们使用Java+Selenium WebDriver 来进行环境的搭建,同样分为两个部分: 安装Java 和 int ...

  10. 如何能够通过代码来重启Android手机?

    String cmd = "su -c reboot";//让手机从启 try { Runtime.getRuntime().exec(cmd); } catch (IOExcep ...