"Required argument not specified" 同时发布 json 日期

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

上下文:我将 Kotlin 与 Micronaut 一起使用,对于这个项目,我不会添加 Spring 依赖项。

我成功创建了一个 Hello Word enpoint,我可以从 Postman post 到它。当我将数据演化为生日 (java.time.LocalDate) 时,问题就开始了。

从 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'\''"
}'

结果

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

实体

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
)

控制器

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 {
    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

整个项目git hub(错误在分支date_issue,不在master)

你应该用@Body注释方法参数:

import io.micronaut.http.annotation.*

// ...


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