本系列文章记录“智能提醒助理”产品建设历程,记录实践经验、巩固知识点、锻炼总结能力。
本篇介绍技术栈升级的过程,遇到的问题和解决方案。
一、需求出发点
智能提醒小程序 当前使用的是jdk8,springboot2.3,升级到jdk21和springboot3.3 学习新知识的同时,感受带来的性能提升。
二、实现路径
先了解其他人的升级经验,获取maven 依赖的版本号。
然后就是 报错替换,最最主要的还是servlet包 变动大。
<properties>
<jdy.version>4.0</jdy.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>21</java.version>
<mybatis-plus.version>3.5.7</mybatis-plus.version>
<dynamic-datasource.version>4.3.1</dynamic-datasource.version>
<pagehelper.boot.version>1.4.7</pagehelper.boot.version>
<druid.version>1.2.23</druid.version>
<bitwalker.version>1.19</bitwalker.version>
<kaptcha.version>2.3.2</kaptcha.version>
<oshi.version>3.9.1</oshi.version>
<commons.io.version>2.15.1</commons.io.version>
<commons.fileupload.version>1.3.3</commons.fileupload.version>
<commons.beanutils.version>1.9.3</commons.beanutils.version>
<jsoup.version>1.18.1</jsoup.version>
<poi.version>3.17</poi.version>
<velocity.version>1.7</velocity.version>
<lombok.version>1.18.34</lombok.version>
<fastjson.version>1.2.83</fastjson.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
</parent>
三、实际操作遇到的问题
1、shiro无springboot3.3版本的支持
先参考以上3篇完成后,基本就能跑起来了。
跑起来 遇到的第一个问题就shiro 与springboot3.3.4 不兼容的问题。
调整了多次无果,最后决定把shiro框架卸载掉,重写了shiro部分,过程比预计的难度要低。
2、swagger API 调整较大
API差别较大,修改工作量大,且当前开发人员少,用不上。考虑先卸载掉,后续需要的时候再加上。
3、springboot3.3.4版本 404 请求报错
升级前:
当前访问一个不存在的资源时会返回404,不会抛出异常。
升级后:
会抛出异常:
[XNIO-1 task-7] ERROR [18d92340fb214056b1e2691fb04aeb36] [c.j.c.c.c.e.GlobalExceptionHandler.handleException-55] - No static resource version.
org.springframework.web.servlet.resource.NoResourceFoundException: No static resource version.
at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.handleRequest(ResourceHttpRequestHandler.java:585)
at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:52)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:527)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:614)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:67)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:67)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:67)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:117)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.SendErrorPageHandler.handleRequest(SendErrorPageHandler.java:52)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:276)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:132)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:256)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:101)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:395)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:859)
at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2513)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1538)
at org.xnio.XnioWorker$WorkerThreadFactory$1$1.run(XnioWorker.java:1282)
at java.base/java.lang.Thread.run(Thread.java:1583)
解决办法:
增加全局异常处理,顺便把其他分类的异常也统一处理下
package com.jdy.common.core.controller.exception;
import com.jdy.common.core.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.NoResourceFoundException;
/**
* 全局异常处理器
*
* @author ruoyi
*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/** 404全局异常处理 */
@ExceptionHandler(NoResourceFoundException.class)
public R handleException(NoResourceFoundException e) {
log.error("请求资源不存在:{}",e.getMessage());
return R.failure("404", "请求资源不存在", "");
}
/** 400 无效请求,参数错误 */
@ExceptionHandler(IllegalArgumentException.class)
public R handleException(IllegalArgumentException e) {
log.error("无效请求,参数错误:{}",e.getMessage());
return R.failure("400", "无效请求,参数错误", "");
}
/** 405请求方式不支持 */
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
public R handleException(HttpRequestMethodNotSupportedException e) {
log.error("请求方式不正确:{}",e.getMessage(), e);
return R.failure("405", "请求资源不支持" + e.getMethod() + "请求", "");
}
/** 500全局异常处理 */
@ExceptionHandler(Exception.class)
public R handleException(Exception e) {
log.error("全局异常:{}",e.getMessage(), e);
return R.failure("500", "服务器忙", "");
}
}
结果展示:
4、pagehelper 分页插件失效
升级前
<!-- pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
升级后
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.7</version>
<exclusions>
<exclusion>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>4.5</version>
</dependency>
需要注意:
第一个参数 要从1开始 而不是0
startPage(page==0 ? 1 : page, size, "id", "DESC");
5、单元测试框架从Junit4升级到了Junit5
org.junit.Test; 升级到了 org.junit.jupiter.api.Test;
升级前:
@RunWith(SpringRunner.class)
@SpringBootTest
升级后:
@RunWith 不再支持。
仅使用 @SpringBootTest 即可
6、容器部署 镜像升级
升级前 11对jdk8是兼容的:
FROM amd64/openjdk:11
升级后:
FROM amd64/openjdk:21-rc-jdk
四、总结
升级后要进行全方面的测试,测试后 还可以通过新老版本并行的方式,观察运行情况,及时调整。