We found following Kotlin code will return null when we call it by http://localhost:8080/message, which should be 'Hello default'
@RestController @RefreshScope open class MessageRestController {
@Value("\${message:Hello default}")
@get:RequestMapping("/message")
internal val message: String? = null
} If we changed it to Java, it works well
used Spring version: spring boot 2.1.6 spring-cloud-dependencies Greenwich.SR3
is this a know issue?
Comment From: mgmeiner
I couldn't reproduce this. Tried it with a simple spring.boot 2.1.6, 2.1.9 and 2.2.0 app. RefreshScope also works fine. Maybe you could create a demo project to reproduce the issue?
Also I would not recommend to use a nullable val
for this because val should not change after initialization. If you want to "inject" the configuration-property like that I would make use of the Kotlin Lateinit Property keyword because the message property can never be null as you provide a default value.
Example:
internal lateinit var message: String
Comment From: newry
Can you try following github link: https://github.com/newry/spring-examples
just follow the readme and you will see the issues Thanks for helping.
Comment From: newry
I found the issue, the method need to be open, otherwise will not work, closing
Comment From: mgmeiner
Consider using the kotlin compiler plugin for spring support. It "opens" the required classes automatically.