gpt4 book ai didi

java - 使用 Jackson 反序列化(未知属性失败)不会忽略鉴别器属性(DTO 使用 SwaggerCodegen 创建)

转载 作者:行者123 更新时间:2023-12-02 02:32:42 24 4
gpt4 key购买 nike

编辑:

我发现了问题:我必须找到一种方法,让 swagger Codegen 在生成 java 类时删除 “visible = true” 部分。如果我手动删除它,它就会起作用。问题是类是在编译时生成的,并且修改将被覆盖。

仍然需要帮助!

初始帖子:

我有以下内容:

  • 具有列表的接待实体类。 Checkpoint 是基类,但它只会包含 Checkpoint1、Checkpoint2 等子类。

  • 具有由“/receptions”映射的 HTTP POST 方法的 ReceptionCotroller。

  • 使用 Swagger CodeGen openapi 3.0.2 生成的接收和检查点的 DTO 类(检查点基类、检查点 1、检查点 2 等)。使用 yml 文件。 Checkpoint DTO 有一个名为“dtype”的鉴别器字段(在 yml 文件中),因此当将 JSON 反序列化为 Checkpoint 时,它知道它引用的子类。

问题是当我添加属性: spring.jackson.deserialization.fail-on-unknown-properties = true 时,它​​无法识别“dtype”属性并且失败。我希望应用程序在遇到未知属性时失败,但忽略“dtype”。

我尝试在 Checkpoint DTO 中添加一个 dtype 字段(在鉴别器定义旁边),但作为响应返回的 JSON 有 2 个 dtype 字段(一个包含鉴别器值,一个包含 null)。

yml 文件中的接收和检查点:

Reception:
description: description1
type: object
properties:
id:
type: integer
format: int64
description: Id of the reception.
checkpoints:
type: array
items:
oneOf:
- $ref: '#/components/schemas/Checkpoint1'
- $ref: '#/components/schemas/Checkpoint2'
discriminator:
propertyName: dtype
$ref: '#/components/schemas/Checkpoint'
description: List of checkpoints attached to this reception.
Checkpoint:
description: Checkpoint entity.
type: object
properties:
checkpointId:
type: string
description: description1.
required:
- checkpointId
- dtype
discriminator:
propertyName: dtype

生成的检查点 DTO 类:

@ApiModel(description = "Checkpoint entity.")
@Validated
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "dtype", visible = true )
@JsonSubTypes({
@JsonSubTypes.Type(value = Checkpoint1.class, name = "Checkpoint1"),
@JsonSubTypes.Type(value = Checkpoint2.class, name = "Checkpoint2")
})
public class Checkpoint {
@JsonProperty("dtype")
private String dtype = null;

@JsonProperty("checkpointId")
private String checkpointId = null;

(.....)

Json 作为请求正文发送:

{
"id": 123,
"checkpoints": [
{
"dtype": "Checkpoint1",
"checkpointId": "Checkpoint1"
}
}

它说它无法识别 dtype 字段。我希望它创建适当的 Checkpoint 对象,但忽略 Checkpoint DTO 实际上在生成的类中并不包含 dtype 字段。我不希望它忽略其他未知属性。例如:如果我在 JSON 中添加另一个“fooBar”字段,我希望它失败。

希望所提供的信息能够理解我的问题所在。如果没有,我可以提供更多信息。

非常感谢!PS:请忽略最终的语法错误或拼写错误,代码可以正常工作并正确复制,但我不知道如何仅忽略鉴别器(dtype)属性。

最佳答案

有两种方法可以完成此任务。
第一:
如果允许更改自动生成的 java 代码,则可以将注释 @JsonIgnoreProperties(value = ["dtype"]) 添加到具有鉴别器字段的所有类,如下所示。

@JsonIgnoreProperties(value = ["dtype"])
public class Checkpoint {
//your properties here
}

示例实现:Use of JsonIgnoreProperties specific property deserialize properties exists only in JSON该问题有一个示例实现

第二:
如果不允许编辑自动生成的 java 代码,您可以使用 Jackson Mixins 来忽略特定不需要的字段。

@JsonIgnoreProperties(value = ["dtype"])
public abstract class CheckpointMixin {}

并在您的对象映射器中为目标类注册此混合,如下所示:

mapper.addMixIn(Checkpoint::class.java, CheckpointMixin::class.java)

我在 kotlin 中尝试了一下,效果符合预期。请忽略任何语法错误。

关于java - 使用 Jackson 反序列化(未知属性失败)不会忽略鉴别器属性(DTO 使用 SwaggerCodegen 创建),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57221327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com