Я следую документации MongoDB на Creating a Custom CodecRegistry
, полезной для object-relational-mapping
.Я попытался реализовать пользовательскую конфигурацию кодека, как показано ниже:
CodecRegistry pojoCodecRegistry = fromRegistries(MongoClient.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
MongoClient mongoClient = new MongoClient("a.b.c", 2345);
MongoDatabase database =
mongoClient.getDatabase("rester").withCodecRegistry(pojoCodecRegistry);
MongoCollection<Record> collection = database.getCollection("cloudtrail",
Record.class);
Block<Record> printBlock = new Block<Record>() {
@Override
public void apply(final Record person) {
System.out.println(person);
}
};
collection.find().forEach(printBlock);
К сожалению, у меня возникла ошибка:
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class de.hpi.aws.model.events.Record.
Вот pojo (класс записи)
public class Record {
private ObjectId Mongoid;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@JsonProperty("eventVersion")
private String eventVersion;
@JsonProperty("userIdentity")
private UserIdentity userIdentity;
@JsonProperty("eventTime")
private String eventTime;
@JsonProperty("eventSource")
private String eventSource;
@JsonProperty("eventName")
private String eventName;
@JsonProperty("awsRegion")
private String awsRegion;
@JsonProperty("sourceIPAddress")
private String sourceIPAddress;
@JsonProperty("userAgent")
private String userAgent;
@JsonProperty("requestParameters")
private RequestParameters requestParameters;
@JsonProperty("additionalEventData")
private AdditionalEventData additionalEventData;
@JsonProperty("requestID")
private String requestID;
@JsonProperty("eventID")
private String eventID;
@JsonProperty("resources")
private List<Resource> resources = new ArrayList<Resource>();
@JsonProperty("eventType")
private String eventType;
@JsonProperty("recipientAccountId")
private String recipientAccountId;
@JsonProperty("sharedEventID")
private String sharedEventID;
/**
* No args constructor for use in serialization
*
*/
public Record() {
}
public Record(String eventVersion, UserIdentity userIdentity, String
eventTime, String eventSource, String eventName, String awsRegion, String
sourceIPAddress, String userAgent, RequestParameters requestParameters,
AdditionalEventData additionalEventData, String requestID, String eventID,
List<Resource> resources, String eventType, String recipientAccountId,
String sharedEventID) {
super();
this.eventVersion = eventVersion;
this.userIdentity = userIdentity;
this.eventTime = eventTime;
this.eventSource = eventSource;
this.eventName = eventName;
this.awsRegion = awsRegion;
this.sourceIPAddress = sourceIPAddress;
this.userAgent = userAgent;
this.requestParameters = requestParameters;
this.additionalEventData = additionalEventData;
this.requestID = requestID;
this.eventID = eventID;
this.resources = resources;
this.eventType = eventType;
this.recipientAccountId = recipientAccountId;
this.sharedEventID = sharedEventID;
}
/**
* setters & getters
*
*/
Я также смотрел на подобные решения, например, объясненное в этом GitHub Gist .Происходит та же ошибка, кажется, что я что-то упускаю, но я пока не смог определить это!