0219 springmvc-拦截器和响应增强
拦截器
拦截器分同步拦截器和异步拦截器;
HandlerInterceptor
方法和执行时机
可以看DispathcerServlet的原来确定它的三个方法的执行时机;
AsynHandlerInterceptor
看注释,主要用来清理在并发环境加清理ThreadLocal的数据;
ResponseBodyAdvice
对返回值备注了@ResponseBody或者返回ResponseEntity做了一些加工;
会在使用消息转换器转换为json数据之前进行数据转换输出;
package com.springbootpractice.interceptor.config;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.springbootpractice.interceptor.config.interceptor.MyInterceptor;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import java.util.HashMap;
import java.util.Map;
/**
* 说明:配置拦截器和设置统一返回格式
* @author carter
* 创建时间: 2020年02月19日 11:03 下午
**/
@Configuration
@ControllerAdvice
public class WebConfig implements WebMvcConfigurer, ResponseBodyAdvice {
@Override
public void addInterceptors(InterceptorRegistry registry) {
HandlerInterceptor myIntercepter = new MyInterceptor() ;
registry.addInterceptor(myIntercepter).addPathPatterns("/**");
}
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
return true;
}
@SneakyThrows
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
Map<String,Object> map = new HashMap();
map.put("result","true");
map.put("data",body);
return new ObjectMapper().writeValueAsString(map);
}
}
小结

通过本小节,你可以学到:
- 如何配置拦截器,打印每个http接口的耗时;
- 如何设置接口的统一返回格式;

原创不易,转载请注明出处。
0219 springmvc-拦截器和响应增强的更多相关文章
- SpringMVC拦截器Interceptor
SpringMVC拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似与servlet中的Filter. SpringMVC 中的Interceptor 拦截请求是通过Ha ...
- Java Servlet 过滤器与 springmvc 拦截器的区别?
前言:在工作中,遇到需要记录日志的情况,不知道该选择过滤器还是拦截器,故总结了一下. servlet 过滤器 定义 java过滤器能够对目标资源的请求和响应进行截取.过滤器的工作方式分为四种 应用场景 ...
- spring boot配置springMVC拦截器
spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...
- SpringMVC之八:基于SpringMVC拦截器和注解实现controller中访问权限控制
SpringMVC的拦截器HandlerInterceptorAdapter对应提供了三个preHandle,postHandle,afterCompletion方法. preHandle在业务处理器 ...
- Springboot中SpringMvc拦截器配置与应用(实战)
一.什么是拦截器,及其作用 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对 ...
- SpringMVC拦截器的配置与使用详解
一.SpringMVC拦截器简介 Spring MVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.在springmvc中,定义拦截 ...
- SpringMVC拦截器+Spring自定义注解实现权限验证
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- springmvc拦截器入门及其执行顺序源码分析
springmvc拦截器是偶尔会用到的一个功能,本案例来演示一个较简单的springmvc拦截器的使用,并通过源码来分析拦截器的执行顺序的控制.具体操作步骤为:1.maven项目引入spring依赖2 ...
- 【SpringMVC】SpringMVC 拦截器
SpringMVC 拦截器 文章源码 拦截器的作用 SpringMVC 的处理器拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理. 谈到拦截器,还有另外一个概 ...
随机推荐
- 使用requests、BeautifulSoup、线程池爬取艺龙酒店信息并保存到Excel中
import requests import time, random, csv from fake_useragent import UserAgent from bs4 import Beauti ...
- centos系统重装python或yum 报There was a problem importing one of the Python modules required to run yum. The error leading to this problem was:错误
sudo vim /usr/bin/yum #修个python所在的路径,例如 #/usr/local/bin/python2.6 或 /usr/local/bin/python2.7要原本你的系统原 ...
- centos输入正确密码后依旧无法登陆问题
输入正确用户名和密码时依旧无法登录. 进入单用户模式重置密码: 开机启动时,按‘E’键(倒计时结束前)进入界面 选择第二项,按‘E’键再次进入 在最后一行添加‘ 1’(空格 1) 回车键保存,回到该界 ...
- vue路由--动态路由
前面介绍的路由都是路径和组件一对一映射的 有时候需要多个路径映射到一个组件,这个组件根据参数的不同动态改变,这时候需要用到动态路由 动态路由这样定义路由路径: path: '/foo/:id'--可以 ...
- C#代码实现-冒泡排序
冒泡排序原理:(升序)通过当前位置数和后一个位置数进行比较 如果当前数比后一个数大 则交换位置, 完成后 比较基数的位置变成下一个数.直到数组末尾,当程序运行完第一遍 最大的数已经排序到最后一个位置了 ...
- 封装好通用的reset.css base.css 样式重置css文件
一般是叫reset.css 我这边命名成base.css 哎呀无所谓…… @charset "UTF-8"; /*css reset*/ /*清除内外边距*/ body, h1, ...
- Android.bp文件简介
Android.bp是用来替换Android.mk的配置文件,它使用Blueprint框架来解析.Blueprint是生成.解析Android.bp的工具,是Soong的一部分.Soong则是专为An ...
- C#的委托案例
C#实现(Delegate)的委托就不多说了,直接上代码,看代码中的注释: namespace Delegate { delegate void DGSayiHi(string name);//声明委 ...
- 程序员为什么害怕低代码?ZT
转自:https://www.jianshu.com/p/cd89fe94cd30 低代码 是一种近些年兴起的企业软件快速开发技术和工具.借助低代码使用者无需编码即可完成企业应用的常用功能,少量编码扩 ...
- 硬盘500M,为什么没有500M。10M宽带,为什么网速没有10M?
在天朝, 硬件厂商用1000代替1024, 通信公司,用 byte来代替bit. 比如 500G的硬盘,应该有 500 * 1024 *1024 *8 = 4.194304*10^9 位 但是按照厂商 ...