java - ObjectMapper 的默认行为

标签 java json objectmapper

我有一个类对象,我正在使用 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/

相关文章:

java - 如果生成字符时发生KEY_TYPED事件,那么为什么KeyEvent的code参数包含VK_UNDEFINED?

Java 一行变量声明?

java - 读取/过滤文本文件的最快方法是什么

python - Scrapy yield 项目作为 JSON 中的子项目

javascript - 成功调用时日期格式不正确

java - ObjectMapper 的两个实例

java - JMS + Wildfly 8.2.0 最终版 : The following keys are invalid for configuring a connector: http-upgrade-endpoint

json - 如何使用 Golang 解码动态 json 对象

ios - AlamofireObjectMapper 如何映射对象?

ios - 是否可以使用 ObjectMapper 创建 JSON?