当前位置: 首页 > 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());}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 力扣题解(不相交的线)
  • Hive中的数据类型和存储格式总结
  • 对接企业微信API自建应用配置企业可信IP
  • [k8s源码]1.client-go集群外部署
  • 函数传值面试题
  • 【postgresql】视图(View)
  • ref 和 reactive 区别
  • Apache Lucene 详解及示例
  • 深入了解MySQL中的innodb_lock_wait_timeout
  • mybatis语法进阶1
  • MySQL数字相关数据处理函数
  • 6-7 宠物领养开发及相关代码
  • Flowable(一个开源的工作流和业务流程管理引擎)中与事件相关的一些核心概念
  • 老年生活照护实训室:让养老护理更个性化
  • vue解决页面放大图片模糊的问题
  • JavaScript 如何正确处理 Unicode 编码问题!
  • es6
  • LeetCode541. Reverse String II -- 按步长反转字符串
  • mysql常用命令汇总
  • seaborn 安装成功 + ImportError: DLL load failed: 找不到指定的模块 问题解决
  • weex踩坑之旅第一弹 ~ 搭建具有入口文件的weex脚手架
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 记录:CentOS7.2配置LNMP环境记录
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 删除表内多余的重复数据
  • 提升用户体验的利器——使用Vue-Occupy实现占位效果
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • 微信小程序设置上一页数据
  • 我感觉这是史上最牛的防sql注入方法类
  • Android开发者必备:推荐一款助力开发的开源APP
  • C# - 为值类型重定义相等性
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • # centos7下FFmpeg环境部署记录
  • #if和#ifdef区别
  • (day18) leetcode 204.计数质数
  • (libusb) usb口自动刷新
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (附源码)ssm本科教学合格评估管理系统 毕业设计 180916
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .NET Framework 和 .NET Core 在默认情况下垃圾回收(GC)机制的不同(局部变量部分)
  • .net FrameWork简介,数组,枚举
  • .NET/C# 异常处理:写一个空的 try 块代码,而把重要代码写到 finally 中(Constrained Execution Regions)
  • .net6+aspose.words导出word并转pdf
  • .stream().map与.stream().flatMap的使用
  • /tmp目录下出现system-private文件夹解决方法
  • [.NET]桃源网络硬盘 v7.4
  • [000-01-022].第03节:RabbitMQ环境搭建
  • [2019/05/17]解决springboot测试List接口时JSON传参异常
  • [3D游戏开发实践] Cocos Cyberpunk 源码解读-高中低端机性能适配策略
  • [Android]RecyclerView添加HeaderView出现宽度问题
  • [c++] 自写 MyString 类