spring boot 项目使用native打包的时候遇到的问题,分享给大家
报错内容:without it being registered for runtime reflection. Add it to the reflection metadata to solve this problem.
The program tried to reflectively invoke method public org.test.controlcenterapi.ws.DocumentEndpoint() without it being registered for runtime reflection. Add it to the reflection metadata to solve this problem. See https://www.graalvm.org/latest/reference-manual/native-image/metadata/#reflection for help.
如果本地运行没有问题,但是却在构建过程中就出现了错误,或者运行过程中出现错误,建议检查一下spring boot版本,在创建项目最开始的时候使用了spring Boot 3.2.4 降版本最后测试这个版本没有问题。
这是我的版本可以参考一下,在项目中,存在的依赖包都有这些postgresql、mongodb、websocket、kafka。
jdk 17
spring Boot 3.2.0
native 0.9.28
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "3.2.0"
id("io.spring.dependency-management") version "1.1.4"
id("org.graalvm.buildtools.native") version "0.9.28"
kotlin("jvm") version "1.9.22"
kotlin("plugin.spring") version "1.9.22"
}
group = "org.gjdsj"
version = "0.0.1-SNAPSHOT"
java {
sourceCompatibility = JavaVersion.VERSION_17
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.jetbrains.kotlin:kotlin-reflect")
testImplementation("org.springframework.boot:spring-boot-starter-test")
implementation("org.postgresql:postgresql:42.5.4")
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
implementation("org.springframework.boot:spring-boot-starter-websocket")
implementation("org.springframework.kafka:spring-kafka")
// https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch
implementation("co.elastic.clients:elasticsearch-java:8.12.2")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
如果项目能够正常构建运行起来,在接口请求的时候报错
The program tried to reflectively invoke method public org.test.controlcenterapi.ws.DocumentEndpoint() without it being registered for runtime reflection. Add it to the reflection metadata to solve this problem. See https://www.graalvm.org/latest/reference-manual/native-image/metadata/#reflection for help.
这种反射错误,controller、entity、vo都回出现错误,他在构建的过程中没有添加进去,需要在构建时告诉native。
官网:https://www.graalvm.org/latest/reference-manual/native-image/metadata/#reflection
解决:reflect-config.json
目录:文件放在 /src/main/resources/META-INF/native-image/
内容:
[
{
"name": "org.test.controlcenterapi.controller.AcquireController",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true,
"queryAllDeclaredMethods": true,
"queryAllDeclaredConstructors": true,
"queryAllPublicMethods": true,
"queryAllPublicConstructors": true,
"unsafeAllocated": true
},
{
"name": "org.test.controlcenterapi.ws.DocumentEndpoint",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true,
"queryAllDeclaredMethods": true,
"queryAllDeclaredConstructors": true,
"queryAllPublicMethods": true,
"queryAllPublicConstructors": true,
"unsafeAllocated": true
},
{
"name": "org.test.controlcenterapi.ws.BigTableEndpoint",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true,
"queryAllDeclaredMethods": true,
"queryAllDeclaredConstructors": true,
"queryAllPublicMethods": true,
"queryAllPublicConstructors": true,
"unsafeAllocated": true
},
{
"name": "org.test.controlcenterapi.vo.DocumentEndpointRequest",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true,
"queryAllDeclaredMethods": true,
"queryAllDeclaredConstructors": true,
"queryAllPublicMethods": true,
"queryAllPublicConstructors": true,
"unsafeAllocated": true
},
{
"name": "org.test.controlcenterapi.vo.BigTableEndpointRequest",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true,
"queryAllDeclaredMethods": true,
"queryAllDeclaredConstructors": true,
"queryAllPublicMethods": true,
"queryAllPublicConstructors": true,
"unsafeAllocated": true
},
{
"name": "org.test.controlcenterapi.controller.GetTableDataController",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true,
"queryAllDeclaredMethods": true,
"queryAllDeclaredConstructors": true,
"queryAllPublicMethods": true,
"queryAllPublicConstructors": true,
"unsafeAllocated": true
},
{
"name": "org.test.controlcenterapi.entity.Index",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true,
"queryAllDeclaredMethods": true,
"queryAllDeclaredConstructors": true,
"queryAllPublicMethods": true,
"queryAllPublicConstructors": true,
"unsafeAllocated": true
},
{
"name": "org.test.controlcenterapi.entity.TableColumn",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true,
"queryAllDeclaredMethods": true,
"queryAllDeclaredConstructors": true,
"queryAllPublicMethods": true,
"queryAllPublicConstructors": true,
"unsafeAllocated": true
},
{
"name": "org.test.controlcenterapi.entity.TableComment",
"allDeclaredMethods": true,
"allDeclaredFields": true,
"allDeclaredConstructors": true,
"allPublicMethods": true,
"allPublicFields": true,
"allPublicConstructors": true,
"queryAllDeclaredMethods": true,
"queryAllDeclaredConstructors": true,
"queryAllPublicMethods": true,
"queryAllPublicConstructors": true,
"unsafeAllocated": true
}
]
为了方便直接无脑全部都是true,如果想探究具体是什么意思可以去官网查看。
在写reflect-config.json的时候不建议把全部文件都添加到里面,只把有问题的类添加到里面就好。