1 前言
JDK8雖然非常好,但是JDK版本已經(jīng)發(fā)布到JDK20了,且JDK8后的版本升級(jí)了很多新的特性,如模塊化、ZGC以及虛擬線程、結(jié)構(gòu)性并發(fā)等,也是非常有吸引力的,所以決定將基于JDK8的項(xiàng)目升級(jí)到最近的LTS版本JDK17。
2 升級(jí)過程記錄
2.1 安裝JDK17
下載JDK17的最新版本jdk-17_linux-x64_bin.tar.gz
,解壓縮后移動(dòng)到/usr/lib/jvm/
目錄下
$ sudo su -
# tar -xzf jdk-17_linux-x64_bin.tar.gz
# mv jdk-17.0.2 /usr/lib/jvm/java-17
復(fù)制代碼
然后修改~/.bashrc
,設(shè)置java相關(guān)環(huán)境變量為JDK17
# vim ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-17
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
復(fù)制代碼
環(huán)境變量生效后,檢查當(dāng)前的jdk版本為JDK17
# source ~/.bashrc
# java -version
openjdk version "17.0.2" 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)
復(fù)制代碼
2.2 升級(jí)spring版本并編譯
修改項(xiàng)目的pom.xml
文件,將spring boot和spring cloud版本由
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
復(fù)制代碼
修改為最新正式發(fā)布版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<spring-cloud.version>2022.0.2</spring-cloud.version>
</properties>
復(fù)制代碼
編譯項(xiàng)目,報(bào)以下錯(cuò)誤:
程序包javax.servlet.http不存在
程序包javax.validation不存在
復(fù)制代碼
原因是原先javax
包的名字改為jakarta
了,將項(xiàng)目中所有依賴javax
包的地方替換為jakarta
繼續(xù)編譯,報(bào)以下錯(cuò)誤:
[ERROR] 找不到符號(hào)
[ERROR] 符號(hào): 類 EnableEurekaClient
[ERROR] 位置: 程序包 org.springframework.cloud.netflix.eureka
復(fù)制代碼
原因是新版本沒有@EnableEurekaClient
注解了,替換為@EnableDiscoveryClient
繼續(xù)編譯,報(bào)以下錯(cuò)誤:
[ERROR] 找不到符號(hào)
[ERROR] 符號(hào): 方法 apply()
[ERROR] 位置: 接口 io.github.resilience4j.core.functions.CheckedSupplier<java.lang.Object>
復(fù)制代碼
原因是resilience4j
的CheckedSupplier
接口新版本沒有apply()
方法了,改為get()
方法
繼續(xù)編譯,報(bào)以下錯(cuò)誤:
[ERROR] 對(duì)于RetryableException(int,java.lang.String,feign.Request.HttpMethod,java.util.Date), 找不到合適的構(gòu)造器
[ERROR] 構(gòu)造器 feign.RetryableException.RetryableException(int,java.lang.String,feign.Request.HttpMethod,java.lang.Throwable,java.util.Date,feign.Request)不適用
[ERROR] (實(shí)際參數(shù)列表和形式參數(shù)列表長(zhǎng)度不同)
[ERROR] 構(gòu)造器 feign.RetryableException.RetryableException(int,java.lang.String,feign.Request.HttpMethod,java.util.Date,feign.Request)不適用
[ERROR] (實(shí)際參數(shù)列表和形式參數(shù)列表長(zhǎng)度不同)
復(fù)制代碼
原因是openfeign
新版本的RetryableException
異常類的構(gòu)造函數(shù)發(fā)生了變化,根據(jù)需要將舊代碼:
@Bean
public ErrorDecoder feignError() {
return (key, response) -> {
if (response.status() >= 500) {
FeignException exception = FeignException.errorStatus(key, response);
return new RetryableException(
response.status(),
exception.getMessage(),
response.request().httpMethod(),
new Date());
}
// 其他異常交給Default去解碼處理
return defaultErrorDecoder.decode(key, response);
};
}
復(fù)制代碼
改為以下代碼
@Bean
public ErrorDecoder feignError() {
return (key, response) -> {
if (response.status() >= 500) {
FeignException exception = FeignException.errorStatus(key, response);
return new RetryableException(
response.status(),
exception.getMessage(),
response.request().httpMethod(),
new Date(),
response.request());
}
// 其他異常交給Default去解碼處理
return defaultErrorDecoder.decode(key, response);
};
}
復(fù)制代碼
改為后繼續(xù)編譯,報(bào)以下錯(cuò)誤:
程序包org.junit不存在
程序包org.junit.runner不存在
程序包junit.framework不存在
復(fù)制代碼
這是因?yàn)榕f版本使用的是junit4
,改為junit5
相應(yīng)的注解。即將:
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@Ignore
@RunWith(MockitoJUnitRunner.class)
public class FileSyncerTest {
@Before
public void setUp() {
}
@Test
public void testCase1() throws Exception {
}
}
復(fù)制代碼
改為
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
@Disabled
@ExtendWith(MockitoExtension.class)
public class FileSyncerTest {
@BeforeEach
public void setUp() {
}
@Test
public void testCase1() throws Exception {
}
}
復(fù)制代碼
改為后繼續(xù)編譯,編譯通過。
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.582 s (Wall Clock)
[INFO] Finished at: 2023-05-04T16:39:42+08:00
[INFO] Final Memory: 59M/214M
[INFO] ------------------------------------------------------------------------
復(fù)制代碼
2.3 啟動(dòng)項(xiàng)目
編譯通過后啟動(dòng)項(xiàng)目,啟動(dòng)失敗,報(bào)以下錯(cuò)誤:
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @7634b327
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
at net.sf.cglib.core.ReflectUtils$2.run(ReflectUtils.java:56)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
at net.sf.cglib.core.ReflectUtils.<clinit>(ReflectUtils.java:46)
復(fù)制代碼
這是因?yàn)閺腏DK9開始支持模塊化了,項(xiàng)目中使用的部分組件可能還沒有支持模塊化,所以需要在jar包啟動(dòng)時(shí)添加add-opens
jvm啟動(dòng)參數(shù)參數(shù),我是通過在pom文件中添加build參數(shù)實(shí)現(xiàn)的:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 添加 add-opens jvm參數(shù) -->
<jvmArguments>--add-opens java.base/java.lang=ALL-UNNAMED</jvmArguments>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
復(fù)制代碼
修改完后重新編譯啟動(dòng),啟動(dòng)仍然失敗,報(bào)以下錯(cuò)誤:
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException: null
復(fù)制代碼
這是因?yàn)轫?xiàng)目中使用了knife4j,由于版本比較低,底層依賴的是spring-fox,支持的是openapi 2.x版本,而spring boot 3.0只支持openapi 3.x版本,所以knife4j版本依賴由:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.5</version>
</dependency>
復(fù)制代碼
改為:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.1.0</version>
</dependency>
復(fù)制代碼
同時(shí)將swagger的相關(guān)注解@Api
、@ApiOperation
、@ApiParam
、@ApiModel
、@ApiModelProperty
替換為openapi3
對(duì)應(yīng)的注解:@Tag
、@Operation
、 @Parameter
、 @Schema
、 @SchemaProperty
修改完后,重新編譯啟動(dòng),這次能正常啟動(dòng)了
但是web訪問項(xiàng)目接口時(shí)報(bào)以下錯(cuò)誤:
Caused by: java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
at org.springframework.web.cors.CorsConfiguration.validateAllowCredentials(CorsConfiguration.java:516)
at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:538)
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1275)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011)
... 36 common frames omitted
復(fù)制代碼
這個(gè)是跨域的問題,新版本spring MVC的CorsRegistry
已經(jīng)沒有allowedOrigin()
方法了,替換為新接口allowedOriginPatterns()
即可,代碼示例如下:
@Configuration
public class WebCorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}
復(fù)制代碼
到此升級(jí)完成!文章來源:http://www.zghlxwxcb.cn/news/detail-505050.html
作者:movee
鏈接:https://juejin.cn/post/7229250736115138621文章來源地址http://www.zghlxwxcb.cn/news/detail-505050.html
到了這里,關(guān)于JDK8升級(jí)JDK17過程中遇到的那些坑的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!