拦截器

拦截器分同步拦截器和异步拦截器;

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);
}
}

小结

通过本小节,你可以学到:

  1. 如何配置拦截器,打印每个http接口的耗时;
  2. 如何设置接口的统一返回格式;

代码点我获取!

原创不易,转载请注明出处。

0219 springmvc-拦截器和响应增强的更多相关文章

  1. SpringMVC拦截器Interceptor

    SpringMVC拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似与servlet中的Filter. SpringMVC 中的Interceptor 拦截请求是通过Ha ...

  2. Java Servlet 过滤器与 springmvc 拦截器的区别?

    前言:在工作中,遇到需要记录日志的情况,不知道该选择过滤器还是拦截器,故总结了一下. servlet 过滤器 定义 java过滤器能够对目标资源的请求和响应进行截取.过滤器的工作方式分为四种 应用场景 ...

  3. spring boot配置springMVC拦截器

    spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...

  4. SpringMVC之八:基于SpringMVC拦截器和注解实现controller中访问权限控制

    SpringMVC的拦截器HandlerInterceptorAdapter对应提供了三个preHandle,postHandle,afterCompletion方法. preHandle在业务处理器 ...

  5. Springboot中SpringMvc拦截器配置与应用(实战)

    一.什么是拦截器,及其作用 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对 ...

  6. SpringMVC拦截器的配置与使用详解

         一.SpringMVC拦截器简介      Spring MVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.在springmvc中,定义拦截 ...

  7. SpringMVC拦截器+Spring自定义注解实现权限验证

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  8. springmvc拦截器入门及其执行顺序源码分析

    springmvc拦截器是偶尔会用到的一个功能,本案例来演示一个较简单的springmvc拦截器的使用,并通过源码来分析拦截器的执行顺序的控制.具体操作步骤为:1.maven项目引入spring依赖2 ...

  9. 【SpringMVC】SpringMVC 拦截器

    SpringMVC 拦截器 文章源码 拦截器的作用 SpringMVC 的处理器拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理. 谈到拦截器,还有另外一个概 ...

随机推荐

  1. 无线网络WPA加密算法基础

    2013-11-13 23:08 (分类:网络安全) 对无线没什么认识,总听说有人蹭网,还有卖蹭网器的,于是补充一下知识. 无线加密有两类:WEP WAP,目前采用WEP加密的非常少了,WEP应该只是 ...

  2. [Linux]命令返回值以及错误对照表

    Linux执行完命令之后默认会有一个返回值 # ls app backupconfig.json Doc manage.py __pycache__ settings.py # echo $? 0 错 ...

  3. js 细节

    1.js中var a=s=1和var a=1,s=1的区别 function fl() { ; } function fl1() { , s = ; } 看出问题所在了吗? var q=w=1 中的s ...

  4. 【C#】写文件时如何去掉编码前缀

    我们都知道,文件有不同的编码,例如我们常用的中文编码有:UTF8.GK2312 等. Windows 操作系统中,新建的文件会在起始部分加入几个字符的前缀,来识别编码. 例如,新建文本文件,写入单词 ...

  5. Sublime Text3添加到右键菜单,"用 SublimeText3 打开"

    在Sublime Text3安装目录下新建一个文件 sublime_addright.inf 文件内容: [Version] Signature="$Windows NT$" [D ...

  6. Verilog HDL学习_1:分频器/PWM的实现

    (一)参考学习资料 (二)实际操作 1. 相关变量计算:   First Initial Second Initial Upper case H X ASCII (Dec) 72 88 Lengths ...

  7. yum的repo的配置文件说明

    [base]:容器名称,一定要放在[]中.name:容器说明,可以自己随便写.mirrorlist:镜像站点,这个可以注释掉.baseurl:我们的 yum 源服务器的地址.默认是 CentOS 官方 ...

  8. c++输入输出,保留几位小数

    #include <iomanip> //头文件 //第一种写法 cout<<setiosflags(ios::); //第二种写法 cout.setf(ios::fixed) ...

  9. 常用Content-type对照表

    文件扩展名 Content-type .html text/html .xhtml text/html .gif image/gif .png image/png .jpg image/jpeg 更加 ...

  10. Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set…

    php打印小票错误提示:Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activate ...