我有一个像这样的 MongoDB 事件实体:
@Getter
@Setter
@Document(collection = "events")
public class Event {
@Id
private String id;
private String event;
private String messageid;
private String userid;
private Properties properties;
private Context context;
private String timestamp;
}
@Getter
@Setter
class Properties {
private String productid;
}
@Getter
@Setter
class Context {
private String source;
}
我有一个 JSON 文件示例:
{"event": "ProductView", "messageid": "b19ac8bb-7011-482d-aeca-a53b93ef277f", "userid": "user-382", "properties": {"productid": "product-354"}, "context": {"source": "desktop"}}
我正在收听 Kafka 主题并使用 mongoDB 的 JSON 行,但在我的集合(由事件命名)中,其显示如下:
但我不想要那些情况; {"source": "mobile-app"} 和 {"productid": "product-340"} 相反,我只想要数据,而不是 JSON 格式。
我找不到解决我的问题的代码块。我应该如何更改我的 Event 类或者我应该使用哪个注释?
如果要将嵌套的
JSON
对象直接映射到 Properties
和 Context
类中的字段,请勿将它们存储为原始 JSON strings
,确保 Spring Data MongoDB
知道如何映射嵌套对象.
您必须确保 Context 和 Properties 类被识别为
MongoDB
中的嵌入文档。使用 @Embedded
或 @Document
注释来注释这些类。
您的固定课程:
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Getter
@Setter
@Document(collection = "events")
public class Event {
@Id
private String id;
private String event;
private String messageid;
private String userid;
private Properties properties;
private Context context;
private String timestamp;
}
@Getter
@Setter
class Properties {
private String productid;
}
@Getter
@Setter
class Context {
private String source;
}