spring boot graalvm native reflect-config.json without it being registered for runtime reflection.

文章讲述了在SpringBoot项目中使用native打包时遇到的runtimereflection问题,涉及到了如何在GraalVM中处理反射,以及如何通过`reflect-config.json`文件配置需要在构建时注册的类。作者建议检查SpringBoot版本,并提供了一份示例配置以解决相关依赖类的反射问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的时候不建议把全部文件都添加到里面,只把有问题的类添加到里面就好。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值