从Kotlin中的Spring配置创建列表映射
我正在尝试在从Kotlin中的Spring配置创建列表映射,spring,spring-boot,kotlin,yaml,Spring,Spring Boot,Kotlin,Yaml,我正在尝试在Spring Boot应用程序中创建Map类型的对象,该应用程序是用Kotlin编写的 我可以从配置创建映射,也可以从配置创建列表,但当我尝试将两者结合起来时,会出现以下异常: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myConfiguration': Could not bind properties to MyConfiguration (pre
Spring Boot
应用程序中创建Map
类型的对象,该应用程序是用Kotlin
编写的
我可以从配置创建映射,也可以从配置创建列表,但当我尝试将两者结合起来时,会出现以下异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myConfiguration': Could not bind properties to MyConfiguration (prefix=, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is java.lang.NullPointerException
我的配置对象
:
@ConfigurationProperties
@Component
class MyConfiguration {
var myNewMap: Map<String, List<String>>? = null
}
用Spring在配置中读取的方式,这可能吗?或者,这是创建一个值为逗号分隔字符串的简单映射并在我的应用程序中将其转换为列表的唯一方法
Kotlin:1.2.0
Spring-Boot:1.5.6.RELEASE
当映射位于建议用于Spring-Boot配置的“前缀”内时,它对我来说效果很好
我的yml:
apiconfig:
myNewMap:
firstKey:
- 2
- 4
secondKey:
- 2
和我的配置类:
@Component
@ConfigurationProperties(prefix = "apiconfig")
class ApiConfiguration {
var myNewMap: Map<String, List<String>>? = null
}
@Component
@ConfigurationProperties(prefix = "apiconfig")
class ApiConfiguration {
var myNewMap: Map<String, List<String>>? = null
}
@RestController
class Apis(@Autowired private val apiConfiguration : ApiConfiguration) {
@GetMapping("/test")
fun test(): String {
apiConfiguration.myNewMap
return "test"
}
}