1

I'm trying to build a simple api with spring boot and kotlin. When I make a request to save a new user everything goes well, a new user is persisted, but response body is empty. I don't know why, I return the new user. Please help me to understand why Jackson isn't serializing my kotlin class.

I can assure that the user returned is not null.

Entity:

@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();
    }
}

Controller:

@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 Answer 1

4

Everything in your data class is private. Typically serialization ignores private members in a class. If you make the necessary fields you desire to get serialized public, or just remove the private keyword I'm betting it works for you.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm feeling very stupid right now haha, thanks for helping me with my problem
Happens to everyone, especially as the new thing comes along.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.