Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何使用Binder反序列化到Kotlin';什么是数据类?_Spring Boot_Kotlin - Fatal编程技术网

Spring boot 如何使用Binder反序列化到Kotlin';什么是数据类?

Spring boot 如何使用Binder反序列化到Kotlin';什么是数据类?,spring-boot,kotlin,Spring Boot,Kotlin,我使用SpringBoot2.0.0.RELEASE、Kotlin1.2.30和Java9 我在配置文件中有以下配置: test: user: id: 111 username: 111 password: 111 addtime: 2018-11-11 11:11:11 User.kt代码如下: data class User( val id: String, val username: String, va

我使用SpringBoot2.0.0.RELEASE、Kotlin1.2.30和Java9

我在配置文件中有以下配置:

test:
  user:
    id: 111
    username: 111
    password: 111
    addtime: 2018-11-11 11:11:11
User.kt代码如下:

data class User(
        val id: String,
        val username: String,
        val password: String,
        val addtime: LocalDateTime
) : Serializable
我尝试使用以下代码读取配置:

Binder.get(this.context.environment)
        .bind("test.user", User::class)
        .orElse(null)
但是,它没有成功,并返回以下错误:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [demo.User]: Illegal arguments for constructor; nested exception is java.lang.IllegalArgumentException: No argument provided for a required parameter: parameter #0 id of fun <init>(kotlin.String, kotlin.String, kotlin.String, java.time.LocalDateTime): demo.User
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:179)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:124)
    at org.springframework.boot.context.properties.bind.JavaBeanBinder$Bean.lambda$getSupplier$0(JavaBeanBinder.java:173)
    at org.springframework.boot.context.properties.bind.JavaBeanBinder$BeanSupplier.get(JavaBeanBinder.java:227)
    at org.springframework.boot.context.properties.bind.JavaBeanBinder$BeanProperty.lambda$getValue$0(JavaBeanBinder.java:303)
    ... 80 more
原因:org.springframework.beans.beans实例化异常:未能实例化[demo.User]:构造函数参数非法;嵌套异常为java.lang.IllegalArgumentException:未为所需参数提供参数:参数#0有趣的id(kotlin.String、kotlin.String、kotlin.String、java.time.LocalDateTime):demo.User
位于org.springframework.beans.BeanUtils.InstanceClass(BeanUtils.java:179)
位于org.springframework.beans.BeanUtils.InstanceClass(BeanUtils.java:124)
位于org.springframework.boot.context.properties.bind.JavaBeanBinder$Bean.lambda$getSupplier$0(JavaBeanBinder.java:173)
位于org.springframework.boot.context.properties.bind.JavaBeanBinder$BeanSupplier.get(JavaBeanBinder.java:227)
位于org.springframework.boot.context.properties.bind.JavaBeanBinder$BeanProperty.lambda$getValue$0(JavaBeanBinder.java:303)
... 80多
完整错误日志:


我怎样才能成功?谢谢您的帮助。

也许此参考会有所帮助,请在定义数据类时尝试添加
@PersistentConstructor
注释: 或者,尝试为每个属性设置默认值。例如:

data class User(
    val id: String = "",
    val username: String = "",
    val password: String = "",
    val addtime: LocalDateTime = LocalDateTime.now()
) : Serializable

某些参数(如enum)可能没有合适的默认参数,因此不适合这样做