hibernatekotlinjava-timemicronautmicronaut-rest

"Required argument not specified" while posting a json with date


Context: I am using Kotlin with Micronaut and for this project I will not add Spring dependencies.

I created successfully a Hello Word enpoint and I can post to it from Postman. The issue started when I evolulated the data to have birthday (java.time.LocalDate).

Copied from Postman

curl --location --request POST 'localhost:8080/accountholders' --header 'Content-Type: application/json' --data-raw '{
    "name": "Jimis",
    "age":42,
    "birthDate":"1978-04-14",
    "category":"'\''Student'\''"
}'

Resulted in

{
    "message": "Required argument [AccountHolder accountHolder] not specified",
    "path": "/accountHolder",
    "_links": {
        "self": {
            "href": "/accountholders",
            "templated": false
        }
    }
}

Entity

import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer
import java.time.LocalDate
import javax.persistence.*


@Entity
data class AccountHolder(@Id
                         @GeneratedValue //(strategy = GenerationType.SEQUENCE, generator = "account_generator")
                         //@SequenceGenerator(name="account_generator", sequenceName = "account_seq")
                         var id: Long? = null,
                         var name: String? = null,
                         var age: Int,
                         @field:JsonFormat(pattern = "yyyy-MM-dd")
                         @field:JsonSerialize(using = LocalDateSerializer::class)
                         @field:JsonDeserialize(using = LocalDateDeserializer::class)
                         var birthDate: LocalDate,
                         var category: Category
)

Controller

import com.mybank.model.AccountHolder
import com.mybank.service.AccountHolderService
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Consumes
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Post
import javax.validation.Valid

@Controller("/accountholders")
class AccountHolderController(private val accountHolderService: AccountHolderService) {

    @Post
    @Consumes(MediaType.APPLICATION_JSON)
    fun addAccountHolder(accountHolder: AccountHolder): AccountHolder {
        return accountHolderService.addAccountHolder(accountHolder)
    }

    @Get
    fun getAccountHolder(id: Long): AccountHolder{
        return accountHolderService.findAccountHolderById(id)
    }
}

gradle.properties dependencies

dependencies {
    kapt("io.micronaut.data:micronaut-data-processor")
    implementation("io.micronaut:micronaut-validation")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut:micronaut-runtime")
    implementation("javax.annotation:javax.annotation-api")
    implementation("io.micronaut:micronaut-http-client")
    implementation("io.micronaut.sql:micronaut-jdbc-hikari")
    implementation("io.micronaut.data:micronaut-data-hibernate-jpa")
    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
    runtimeOnly("com.h2database:h2")
}

gradle.properties

micronautVersion=2.1.3
kotlinVersion=1.4.10

The whole project git hub (the error in the branch date_issue, not in the master)


Solution

  • You should annotate the method parameter with @Body:

    import io.micronaut.http.annotation.*
    
    // ...
    
    
    @Post
    @Consumes(MediaType.APPLICATION_JSON)
    fun addAccountHolder(@Body accountHolder: AccountHolder): AccountHolder {
        return accountHolderService.addAccountHolder(accountHolder)
    }