【发布时间】:2019-10-17 11:29:36
【问题描述】:
我有一个监听队列的监听器。来自队列的消息是 json 文本。我需要处理它们,然后保存在 mongodb 数据库中。我已经为传入的 json 使用了 DTO。问题是我只能将数据保存为小写,因为我使用了 DTO。但是,传入的数据是大写的。如何使用 jackson/spring 优雅地做到这一点?
我在 DTO 中尝试了 @JsonGetter 和 @JsonSetter。但是,那没有用。它仍然将数据保存为小写。
我的代码的迷你版:
DTO:
public String getMessage() {
return message;
}
@JsonSetter("MESSAGE")
public void setMessage(String message){
this.message = message;
}
数据保存器:
mongoOperations.save(DTO,collectionname);
数据库中的文档:
_id: ObjectId("5da831183852090ddc7075fb")
message: "hi"
我希望 mongodb 中的数据为:
_id: ObjectId("5da831183852090ddc7075fb")
MESSAGE: "hi"
传入数据的键为 MESSAGE。所以,我想存储相同的数据。我不希望 DTO 字段名称为大写。
【问题讨论】:
-
你在球场上试过
@JsonProperty("MESSAGE")吗? When is the @JsonProperty property used and what is it used for? -
在mongoDB中以大写字母存储MESSAGE的目的是什么?
-
@K.D 这是业务需求。
-
@MichałZiober 是的。我有。但是,我同时使用了 JsonProperty 和 JsonSetter。有错吗?
-
@SangamesKumar,如果您想将其存储在
MongoDB中,请使用适当的annotation。MongoDB可能无法识别Jackson注释。看看:Mapping Annotation Overview,SpringData Mongo @Column equivalent annotation (@Property?)