springboot2.0拦截器配置

springboot中配置拦截的三种方式:
第一种:继承 WebMvcConfigurerAdapter类
在SpringBoot2.0及Spring 5.0 WebMvcConfigurerAdapter已被废弃在这里插入图片描述
第二种:继承WebMvcConfigurationSupport类
在这里插入图片描述这种方式会出现返回的数据为XML格式

(1)响应头
在这里插入图片描述(2)返回值
在这里插入图片描述
针对这种情况我们的解决方案:
1.在前端请求头中加入参数accept = application/json
2.web控制层@RequestMapping中中添加produces = { “application/json; charset=UTF-8” }
3.在拦截器配置中加入内容协商配置代码(根据WebMvcAutoConfiguration改写的)
在这里插入图片描述

第三种:实现WebMvcConfigurer接口,
在这里插入图片描述 上述三种方式中第一种已经过时不推荐使用,第二种配置比较繁琐,第三种比较简单推荐使用

猜你喜欢

转载自blog.csdn.net/yangcheng6327/article/details/88177225
今日推荐