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