Spring引导不是序列化kotlin类

时间:2018-02-13 16:38:08

标签: spring spring-boot jackson kotlin

我正在尝试使用spring boot和kotlin构建一个简单的api。当我提出保存新用户的请求时,一切顺利,新用户被保留,但响应正文为空。我不知道为什么,我返回新用户。请帮助我理解为什么杰克逊没有将我的kotlin课程序列化。

我可以确保返回的用户不是null。

实体:

@Entity
data class User(@Id
                @GeneratedValue(strategy = GenerationType.IDENTITY)
                private val id: Long = 0L,
                private var createdAt: LocalDateTime = LocalDateTime.now(),
                private var enabled: Boolean = false,
                private val username: String = "",
                private val password: String = "",
                private val name: String = "") {

    @PrePersist
    private fun onCreate() {
        this.enabled = true
        this.createdAt = LocalDateTime.now();
    }
}

控制器:

@RestController
@RequestMapping("users")
class UserController {

    @Autowired
    private lateinit var userService: UserService

    @PostMapping
    fun save(@RequestBody user: User): User {
        return this.userService.save(user)
    }
}

的build.gradle:

buildscript {
    ext {
        kotlinVersion = '1.2.20'
        springBootVersion = '2.0.0.RC1'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
    }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'br.com'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinOptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    compile("org.jetbrains.kotlin:kotlin-reflect")
    //JACKSON
    compile "com.fasterxml.jackson.core:jackson-annotations"
    compile "com.fasterxml.jackson.core:jackson-core"
    compile "com.fasterxml.jackson.core:jackson-databind"
    runtime "com.fasterxml.jackson.datatype:jackson-datatype-jdk8"
    runtime "com.fasterxml.jackson.datatype:jackson-datatype-jsr310"
    runtime "com.fasterxml.jackson.module:jackson-module-kotlin"
    runtime('mysql:mysql-connector-java')
    runtime('org.springframework.boot:spring-boot-devtools')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

application.properties

#DATA SOURCE
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc\:mysql\://localhost:3307/kotlin-library
spring.datasource.username = root
spring.datasource.password =

#JPA
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

#SERVER
server.servlet.context-path=/

#JACKSON
spring.jackson.serialization.indent-output=true
spring.jackson.serialization.write-dates-as-timestamps=false
spring.jackson.serialization.write-durations-as-timestamps=false

1 个答案:

答案 0 :(得分:2)

数据类中的所有内容都是私有的。通常,序列化会忽略类中的私有成员。如果你想要公开序列化必要的字段,或者只是删除私有关键字,我认为它适用于你。