【重构小程序】升级JDK1.8、SpringBoot2.x 到JDK17、Springboot 3.x(一)

前言

最近想着把大火的deepseek 迁移到小程序里,基于刷题小程序的数据库做一个RAG应用,来进一步扩展答案解析,帮助用户解答相关问题。但是由于之前做的项目都要老了,并不支持spring 的AI模块,因此,我打算先升级一下系统。

一、升级JDK 1.8 到 JDK 17

1、首先从官网上下载一个JDK17的包,windows系统可能有这两种包(压缩包和安装包)

直接下载压缩包,放在一个中文目录下,然后修改环境变量。

然后看一下java版本 java --version 是否修改成功。

修改项目中的设置 如下

二、升级Springboot 2.x 到 Springboot 3.x

修改pom文件

XML 复制代码
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/>
    </parent>
XML 复制代码
    <properties>
        <java.version>17</java.version>
    </properties>

这里面需要注意的东西还挺多的

1、javax.servlet.*相关的类找不到,需要切换依赖为jakarta.servlet。修改javax.servlet.*为jakarta.servlet.*。

XML 复制代码
		<!--jakarta.servlet start -->
		<dependency>
			<groupId>jakarta.servlet</groupId>
			<artifactId>jakarta.servlet-api</artifactId>
		</dependency>
		<!--jakarta.servlet end -->

2.、mybatis-plus-boot-starter

XML 复制代码
		<!-- mybatis-plus start-->
		<dependency>
		    <groupId>com.baomidou</groupId>
		    <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
		    <version>3.5.5</version>
		</dependency>
		<!-- mybatis-plus end-->

3、redis

bash 复制代码
spring.data.redis.host=127.0.0.1
#Redis服务器连接端口
spring.data.redis.port=6379
#连接池最大连接数(使用负值表示没有限制)
spring.data.redis.lettuce.pool.max-active=20
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.data.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接
spring.data.redis.lettuce.pool.max-idle=5
#连接池中的最小空闲连接
spring.data.redis.lettuce.pool.min-idle=0
#连接超时时间(毫秒)
spring.data.redis.timeout=1800000

4、commons-pool2

Spring Boot 的 spring-boot-starter-data-redis 依赖了 Lettuce 作为 Redis 客户端。

Lettuce 依赖于 commons-pool2 来实现连接池功能。

因此我们也需要更新一下commons-pool2的版本。

XML 复制代码
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.11.1</version>
        </dependency>

4、Spring-Security

咋WebSecurityConfig显示的注册authenticationManager为一个Bean

java 复制代码
    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
        return config.getAuthenticationManager();
    }

使用SecurityFilterChain替代WebSecurityConfigurerAdapter;

java 复制代码
@Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http,
                                                   CustomizeAuthenticationEntryPoint customizeAuthenticationEntryPoint,
                                                   CustomizeAccessDeniedHandler customizeAccessDeniedHandler) throws Exception {

        http
                .sessionManagement(session -> session
                        .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
                )
                .authenticationProvider(thirdLoginAuthenticationProvider)
                .authorizeHttpRequests(authorize -> authorize
                        .requestMatchers(
                                "/swagger-resources/configuration/ui",
                                "/swagger-resources",
                                "/swagger-resources/configuration/security",
                                "/swagger-ui.html",
                        ).permitAll()
                        .anyRequest().authenticated()
                )
                .exceptionHandling(exception -> exception
                        .accessDeniedHandler(customizeAccessDeniedHandler)
                        .authenticationEntryPoint(customizeAuthenticationEntryPoint)
                )
                .cors(cors -> cors.configure(http))
                .csrf(csrf -> csrf.disable());

        return http.build();
    }

Spring-Security 6.x的版本中,对于session的管理也发生了一些变化,需要手动将 SecurityContext 保存到 HttpSession 中。

java 复制代码
        HttpSession session = request.getSession();
        session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext());

修改到这里位置,就可以启动项目了。

相关推荐
小程故事多_801 小时前
从基础Agent到复杂工作流,LangGraph如何用状态机重构智能体开发
人工智能·设计模式·重构·aigc·ai编程
charlie1145141911 小时前
嵌入式C++工程实践——第13篇:第一次重构 —— enum class取代宏,类型安全的开始
开发语言·c++·vscode·stm32·安全·重构·现代c++
黑牛儿3 小时前
SaaS 末日?AI Agent 重构企业服务:案例 \+ 架构 \+ 落地步骤
人工智能·重构·架构
猴哥聊项目管理5 小时前
IPD并行工程模式下的流程重构策略
重构·项目管理·项目经理·华为ipd·研发团队·ipd需求管理·ipd并行工程
疯狂打码的少年5 小时前
【Day14 Java转Python】从Java到Python——用Python重构一个Java小工具(文件批量重命名实战)
java·python·重构
三维频道6 小时前
光学像素重构物理真实:极限工况下的 DIC 全场测量逻辑
数码相机·重构·全场应变测量·数字图像相关技术·可靠性测试·cae仿真对比·无损检测
The Open Group6 小时前
数据资产与TOGAF® |TOGAF如何重构数据治理体系
大数据·人工智能·重构
jinanwuhuaguo7 小时前
Ollama 全方位深度剖析:大模型时代的“Docker化”革命、算力普惠基础设施与安全边界重构
运维·开发语言·人工智能·深度学习·安全·docker·重构
信创DevOps先锋1 天前
2025项目管理工具生态革命:AI重构协作边界与国产化崛起
人工智能·重构
cd_949217211 天前
灵析数智:以 AI GEO 重构品牌增长,领跑生成式引擎优化新赛道
人工智能·搜索引擎·重构