当前位置: 首页 > news >正文

Spring boot 2.0 升级到 3.3.1 的相关问题 (一)

文章目录

  • Spring boot 2.0 升级到 3.3.1 的相关问题 (一)
    • 拦截器Interceptor的变动
      • 问题介绍
      • 解决方案
    • WebMvcConfigurerAdapter 自定义Mvc配置
      • 问题介绍
      • 解决方案

Spring boot 2.0 升级到 3.3.1 的相关问题 (一)

拦截器Interceptor的变动

问题介绍

在2.0 版本可以通过继承org.springframework.web.servlet.handler.HandlerInterceptorAdapter 类来实现一个拦截器,在2.4.0 版本开始标记为弃用,在3.3.1 版本已经没有这个类了,需要使用新的方式来实现。

解决方案

直接实现 org.springframework.web.servlet.HandlerInterceptor 接口即可。

原代码:

import com.abc.springboot.frame.constant.FrameConstant;
import com.abc.springboot.frame.pojo.dto.SystemSecurityRequestDTO;
import com.abc.springboot.frame.utils.RequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** 检查客户端版本号拦截器*/
@Slf4j
public class CheckClientVersionInterceptor extends HandlerInterceptorAdapter {/*** 检查客户端版本是否有效*/@Autowiredprivate ICheckClientVersionHandler checkClientVersionHandler;/*** 请求处理前处理* @param request* @param response* @param handler* @return* @throws Exception*/@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {//获取请求参数SystemSecurityRequestDTO requestDTO = RequestUtils.getAndSetSystemSecurityRequestDTO(request);//校验客户端版本号try{boolean checkResult =  checkClientVersionHandler.checkClientVersion(requestDTO,request.getHeader(FrameConstant.HTTP_HEADER_CLIENT_VERSION),request.getHeader(FrameConstant.HTTP_HEADER_CLIENT_TYPE));if(!checkResult){log.info("版本号不支持【{}】【{}】",requestDTO.getMethod(),requestDTO.getUri());request.getRequestDispatcher(FrameConstant.APPLICATION_URL_CLIENT_VERSION_VERIFY_FAILED).forward(request, response);return false;}return true;}catch (Exception e){log.warn("记录系统请求日志失败。",e);return false;}}
}

新代码


import com.abc.springboot.frame.constant.FrameConstant;
import com.abc.springboot.frame.pojo.dto.SystemSecurityRequestDTO;
import com.abc.springboot.frame.utils.RequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;/*** 检查客户端版本号拦截器*/
@Slf4j
public class CheckClientVersionInterceptor implements HandlerInterceptor {/*** 检查客户端版本是否有效*/@Autowiredprivate ICheckClientVersionHandler checkClientVersionHandler;/*** 请求处理前处理* @param request* @param response* @param handler* @return* @throws Exception*/@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {//获取请求参数SystemSecurityRequestDTO requestDTO = RequestUtils.getAndSetSystemSecurityRequestDTO(request);//校验客户端版本号try{boolean checkResult =  checkClientVersionHandler.checkClientVersion(requestDTO,request.getHeader(FrameConstant.HTTP_HEADER_CLIENT_VERSION),request.getHeader(FrameConstant.HTTP_HEADER_CLIENT_TYPE));if(!checkResult){log.info("版本号不支持【{}】【{}】",requestDTO.getMethod(),requestDTO.getUri());request.getRequestDispatcher(FrameConstant.APPLICATION_URL_CLIENT_VERSION_VERIFY_FAILED).forward(request, response);return false;}return true;}catch (Exception e){log.warn("记录系统请求日志失败。",e);return false;}}
}

WebMvcConfigurerAdapter 自定义Mvc配置

问题介绍

在2.0 版本可以通过继承org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter 类来实现自定义Mvc拦截器,在2.4.0 版本开始标记为弃用,在3.3.1 版本已经没有这个类了,需要使用新的方式来实现。

解决方案

org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter 类在 Spring Framework 5.0 之后被标记为已弃用,并在 Spring Boot 2.0 中不再推荐使用 。

替代方案有两种:

直接实现 WebMvcConfigurer 接口:
这是官方推荐的替代方法。WebMvcConfigurer 接口提供了多种默认方法(即带有实现的方法),允许开发者只实现所需的配置方法,而不必要实现接口中的所有方法。这种方式不会影响 Spring Boot 自身的 @EnableAutoConfiguration,允许 Spring Boot 的自动配置生效 。

继承 WebMvcConfigurationSupport 类:
另一种方法是继承 WebMvcConfigurationSupport 类。这个类提供了 Spring MVC 的默认配置,通过继承它,可以覆盖特定的方法来自定义配置。但请注意,使用这种方式将覆盖 Spring Boot 的自动配置,因此如果某个方法没有被重写,可能会导致相关功能的缺失,比如静态资源的处理 。

总结来说,如果你需要进行一些简单的自定义配置,并且想要保留 Spring Boot 的自动配置功能,推荐直接实现 WebMvcConfigurer 接口。如果你需要更全面的控制 Spring MVC 的配置,可以考虑继承 WebMvcConfigurationSupport 类,但要确保所有必要的配置都被正确覆盖和实现。

原代码

import com.abc.utils.formatter.LocalDateTimeFormatter;
import com.abc.utils.formatter.StringFormatter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import java.time.LocalDateTime;/*** 自定义的Mvc配置,用于配置格式化程序* @author 徐明龙 XuMingLong 2022-03-17*/
@Configuration
public class CustomWebMvcFormattersConfigurer extends WebMvcConfigurerAdapter  {@Overridepublic void addFormatters(FormatterRegistry registry) {//仅对Path方式传入的参数生效registry.addFormatterForFieldType(String.class, new StringFormatter());registry.addFormatterForFieldType(LocalDateTime.class, new LocalDateTimeFormatter());}
}

新代码

import com.abc.utils.formatter.LocalDateTimeFormatter;
import com.abc.utils.formatter.StringFormatter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import java.time.LocalDateTime;/*** 自定义的Mvc配置,用于配置格式化程序* @author 徐明龙 XuMingLong 2022-03-17*/
@Configuration
public class CustomWebMvcFormattersConfigurer implements WebMvcConfigurer {@Overridepublic void addFormatters(FormatterRegistry registry) {//仅对Path方式传入的参数生效registry.addFormatterForFieldType(String.class, new StringFormatter());registry.addFormatterForFieldType(LocalDateTime.class, new LocalDateTimeFormatter());}
}
http://www.lryc.cn/news/400331.html

相关文章:

  • 数据分析——Python网络爬虫(四){爬虫库的使用}
  • C++客户端Qt开发——信号和槽
  • 基于双向长短期记忆 BiLSTM 实现股票单变量时间序列预测(PyTorch版)
  • 微信小程序毕业设计-汽车维修项目管理系统项目开发实战(附源码+论文)
  • 学习大数据DAY16 PLSQL基础语法5
  • LabVIEW心电信号自动测试系统
  • 最值得推荐的10款Windows软件!
  • 游戏视频是后期配音好还是边录边配 游戏视频怎么剪辑制作才能火 视频剪辑免费软件
  • 配置 Node.js 内存限制
  • ORA-12518: TNS: 监听程序无法分发客户机连接
  • 2.5 计算机网络
  • 同三维T80004ESL编码器视频使用操作说明书:高清HDMI编码器,高清SDI编码器,4K超清HDMI编码器,双路4K超高清编码器
  • 「ETL趋势」分区支持PostgreSQL、Greenplum、Gauss200, 定时任务支持Kettle
  • vue 前端项目调用后端接口记录
  • 4.10、matlab生成脉冲序列:pulstran()函数
  • 【JAVA poi-tl-ext 富文本转word】
  • uniapp 小程序注册全局弹窗组件(无需引入,无需写标签)
  • python 语法学习 day 7
  • 【高中数学/幂函数】比较a=2^0.3,b=3^0.2,c=7^0.1的大小
  • 双向带头循环链表
  • 探索TASKCTL和 DataStage 的ETL任务调度协同
  • Facebook软体机器人与机器人框架:创新社交互动的未来
  • 掌握音视频转换的艺术:用FFmpeg解锁多媒体的无限可能
  • C基础day9
  • 32. 小批量梯度下降法(Mini-batch Gradient Descent)
  • MySQL第八次作业
  • 【合集】临时邮箱网站 临时邮箱API(持续更新)
  • 职场新人感受
  • Window 下Mamba 环境安装踩坑问题汇总及解决方法 (无需绕过selective_scan_cuda)
  • 前端项目本地的node_modules直接上传到服务器上无法直接使用(node-sasa模块报错)