如何在 Jackson (2.12.0) 和 Quarkus (1.11.1) 中使用 Kotlin (1.4.21) 数据的默认值 class
How to use default values of Kotlin (1.4.21) data class in Jackson (2.12.0) and Quarkus (1.11.1)
我正在将 Quarkus 1.11.1 与 Kotlin 1.4.21 和 Jackson 2.12.0 一起使用。
我不明白为什么当我发送 POST 请求时,其数据主体 class 具有已定义的默认参数,但不被接受并且 returns错误问题:指定为非空的参数为空
在 pom.xml 文件中我有:
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson</artifactId>
</dependency>
Quarkus 文档说 (https://quarkus.io/guides/kotlin#kotlin-and-jackson):
If the com.fasterxml.jackson.module:jackson-module-kotlin dependency and the quarkus-jackson extension (or the quarkus-resteasy-extension) have been added to project, then Quarkus automatically registers the KotlinModule to the ObjectMapper bean (see this guide for more details).
我有一个数据 class 比如:
data class MyAttributes
@BsonCreator constructor(
@BsonProperty("myId")
@JsonProperty("myId")
var myId: String,
@BsonProperty("name")
@JsonProperty("name")
val name: String,
@BsonProperty("data")
@JsonProperty("data", defaultValue = "{}")
var data: MutableMap<String, Any> = mutableMapOf()
)
我注意到@JsonProperty
注解中的defaultValue没有用,因为它只用于document预期值 (https://fasterxml.github.io/jackson-annotations/javadoc/2.12/com/fasterxml/jackson/annotation/JsonProperty.html#defaultValue--)
如果我发送 JSON 像:
{
"myId": "AB123",
"name": "my attribute name"
}
我收到前面描述的错误,data 字段的默认值被忽略。
如果我发送:
{
"myId": "AB123",
"name": "my attribute name",
"data": {}
}
我没有收到错误,因为我还发送了 data 字段。
你能告诉我我哪里做错了吗?
谢谢
你有默认构造函数吗?默认情况下,杰克逊需要一个默认构造函数。这在数据 类 中并不常见,因此您可以提供构造函数或执行以下操作:
@Bean
fun objectMapper(): ObjectMapper = ObjectMapper()
.registerKotlinModule()
我正在将 Quarkus 1.11.1 与 Kotlin 1.4.21 和 Jackson 2.12.0 一起使用。
我不明白为什么当我发送 POST 请求时,其数据主体 class 具有已定义的默认参数,但不被接受并且 returns错误问题:指定为非空的参数为空
在 pom.xml 文件中我有:
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson</artifactId>
</dependency>
Quarkus 文档说 (https://quarkus.io/guides/kotlin#kotlin-and-jackson):
If the com.fasterxml.jackson.module:jackson-module-kotlin dependency and the quarkus-jackson extension (or the quarkus-resteasy-extension) have been added to project, then Quarkus automatically registers the KotlinModule to the ObjectMapper bean (see this guide for more details).
我有一个数据 class 比如:
data class MyAttributes
@BsonCreator constructor(
@BsonProperty("myId")
@JsonProperty("myId")
var myId: String,
@BsonProperty("name")
@JsonProperty("name")
val name: String,
@BsonProperty("data")
@JsonProperty("data", defaultValue = "{}")
var data: MutableMap<String, Any> = mutableMapOf()
)
我注意到@JsonProperty
注解中的defaultValue没有用,因为它只用于document预期值 (https://fasterxml.github.io/jackson-annotations/javadoc/2.12/com/fasterxml/jackson/annotation/JsonProperty.html#defaultValue--)
如果我发送 JSON 像:
{
"myId": "AB123",
"name": "my attribute name"
}
我收到前面描述的错误,data 字段的默认值被忽略。 如果我发送:
{
"myId": "AB123",
"name": "my attribute name",
"data": {}
}
我没有收到错误,因为我还发送了 data 字段。
你能告诉我我哪里做错了吗?
谢谢
你有默认构造函数吗?默认情况下,杰克逊需要一个默认构造函数。这在数据 类 中并不常见,因此您可以提供构造函数或执行以下操作:
@Bean
fun objectMapper(): ObjectMapper = ObjectMapper()
.registerKotlinModule()