【问题标题】:Spring boot isn't serializing kotlin classSpring Boot 没有序列化 kotlin 类
【发布时间】:2018-07-24 02:25:50
【问题描述】:

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

我可以保证返回的用户不为空。

实体:

@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

【问题讨论】:

    标签: spring spring-boot jackson kotlin


    【解决方案1】:

    您的数据类中的所有内容都是私有的。通常,序列化会忽略类中的私有成员。如果您将希望序列化的必要字段设为公开,或者只是删除私有关键字,我打赌它对您有用。

    【讨论】:

    • 我现在感觉很傻哈哈,谢谢你帮我解决我的问题
    • 每个人都会遇到,尤其是当新事物出现时。
    猜你喜欢
    • 2020-02-22
    • 2021-03-30
    • 2019-04-21
    • 2021-05-16
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 2019-10-03
    • 2017-08-24
    相关资源
    最近更新 更多