gpt4 book ai didi

java - ObjectMapper 的默认行为

转载 作者:行者123 更新时间:2023-11-30 02:05:07 25 4
gpt4 key购买 nike

我有一个类对象,我正在使用 jackson ObjectMapper 将其转换为 json。转换后,它会对每个变量进行两次输入。这种行为正常吗?如果是的话有人可以向我解释一下吗?

我目前的理解是对象映射器采用@JsonProperty注释来创建fieldNames

@Data
@Entity
@Table(name = "oracle_blob")
public class OracleBlob {

@Id
@GenericGenerator(name = "native", strategy = "native")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
@Column(name ="id")
@JsonIgnore
private Long id;

@Column(name ="source_entity")
@JsonProperty("Source_Entity")
private String Source_Entity;

@Column(name ="interface_name")
@JsonProperty("Interface_Name")
private String Interface_Name;

@Column(name ="batch_id")
@JsonProperty("Batch_Id")
private String Batch_Id;

@Column(name ="message")
@JsonProperty("Message_Content")
private String Message_Content;

}

输出

{
"source_Entity":"test source2",
"interface_Name":"test Interface Name2",
"batch_Id":"testbatchId2",
"message_Content":"test message2",
"Source_Entity":"test source2",
"Interface_Name":"test Interface Name2",
"Batch_Id":"testbatchId2",
"Message_Content":"test message2"
}

最佳答案

字段名称不要使用大写字母,因为 Jackson 将解析 getter(在您的示例中由 lombok 生成)并根据 getInterface_Name() + 1 搜索 interface_name @JsonProperty("Interface_Name") 等字段

Jackson 2.5 did add MapperFeature.USE_STD_BEAN_NAMING, enabling of which gets handling you want. It is disabled by default for backwards-compatibility reasons. Another alternative (and only choice if you are using an earlier version) would be to annotate getter with @JsonProperty as well; if so, field and getter would be properly coupled.

在此处查看完整答案:https://github.com/FasterXML/jackson-databind/issues/729#issuecomment-84761480

关于java - ObjectMapper 的默认行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51614666/

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