java litjson,在IntelliT Idea for Kotlin @ConfigurationProperties类中不生成spring-configuration-metadata.js...

在Spring Boot项目中,使用Java配置类可以自动生成spring-configuration-metadata.json文件,但使用Kotlin类时则无法生成。问题在于Spring Boot的配置处理器不支持Kotlin类。解决方案是在build.gradle文件中添加kapt依赖,并在IntelliJ IDEA中启用注解处理。完成这些步骤后,即使使用Kotlin类,也能正确生成配置元数据文件。

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

I'm trying to generate spring-configuration-metadata.json file for my Spring Boot based project. If I use Java @ConfigurationProperties class it is generated correctly and automatically:

@ConfigurationProperties("myprops")

public class MyProps {

private String hello;

public String getHello() {

return hello;

}

public void setHello(String hello) {

this.hello = hello;

}

}

But if I use Kotlin class the spring-configuration-metadata.json file is not generated (I've tried both gradle build and Idea Rebuild Project).

@ConfigurationProperties("myprops")

class MyProps {

var hello: String? = null

}

AFAIK Kotlin generates the same class with constructor, getters and setters and should act as regular Java bean.

Any ideas why spring-boot-configuration-processor doesn't work with Kotlin classes?

解决方案

Thank you for pointing me in the right direction. So the solution is to add

dependencies {

...

kapt "org.springframework.boot:spring-boot-configuration-processor"

optional "org.springframework.boot:spring-boot-configuration-processor"

...

}

to build.gradle file, run gradle compileJava in command line and turn on annotation processing in IntelliJ Idea settings Build, Execution, Deployment -> Compiler -> Annotation processor -> Enable anotation processing. The rest of configuration remains the same

Also note that without this line

optional "org.springframework.boot:spring-boot-configuration-processor"

IntelliJ Idea will complain whith

Cannot resolve configuration property

message in your application.properties or application.yml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值