pet*_*ric 7 spring yaml kotlin spring-boot
我试图Map<String, List<String>>在Spring Boot编写的应用程序中创建一个类型的对象Kotlin.
我能够从config创建一个映射,并且还能够从config创建一个列表,但是当我尝试将两者合并时,我得到以下异常:
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
Run Code Online (Sandbox Code Playgroud)
我的Configuration Object:
@ConfigurationProperties
@Component
class MyConfiguration {
var myNewMap: Map<String, List<String>>? = null
}
Run Code Online (Sandbox Code Playgroud)
我的Configuration yml:
---
myNewMap:
firstKey:
- 2
- 4
secondKey:
- 2
Run Code Online (Sandbox Code Playgroud)
这是否可以通过Spring读取配置的方式实现?或者是创建一个简单的Map的唯一方法,将值作为逗号分隔的字符串,并将其转换为我的应用程序中的列表?
Kotlin: 1.2.0
Spring Boot: 1.5.6.RELEASE
当地图位于推荐用于 Spring Boot 配置的“前缀”内时,它对我来说效果很好。
我的yml:
apiconfig:
myNewMap:
firstKey:
- 2
- 4
secondKey:
- 2
Run Code Online (Sandbox Code Playgroud)
和我的配置类:
@Component
@ConfigurationProperties(prefix = "apiconfig")
class ApiConfiguration {
var myNewMap: Map<String, List<String>>? = null
}
Run Code Online (Sandbox Code Playgroud)
使用类别:
@RestController
class Apis(@Autowired private val apiConfiguration : ApiConfiguration) {
@GetMapping("/test")
fun test(): String {
apiConfiguration.myNewMap
return "test"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
488 次 |
| 最近记录: |