作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 POJO 类,看起来像这样-
public class CacheEntity<V> {
private String cacheId;
private V value;
public static final String ID_KEY = "cacheId";
public CacheEntity() {
}
public CacheEntity(String cache_id, V value) {
this.cacheId = cache_id;
this.value = value;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
public String getCacheId() {
return cacheId;
}
public void setCacheId(String cacheId) {
this.cacheId = cacheId;
}
}
我使用的是官方驱动 mongodb-driver-sync version 3.8.1
如文档中所述 here ,我正在使用带有自动 PojoCodecProvider 的 CodecRegistry。当我尝试保留此实体时出现以下错误。
Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: CacheEntity contains generic types that have not been specialised.
Top level classes with generic types are not supported by the PojoCodec.
at org.bson.codecs.pojo.PojoCodecImpl.encode(PojoCodecImpl.java:93)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
at com.mongodb.operation.BulkWriteBatch$WriteRequestEncoder.encode(BulkWriteBatch.java:398)
at com.mongodb.operation.BulkWriteBatch$WriteRequestEncoder.encode(BulkWriteBatch.java:377)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
at com.mongodb.internal.connection.BsonWriterHelper.writeDocument(BsonWriterHelper.java:75)
at com.mongodb.internal.connection.BsonWriterHelper.writePayload(BsonWriterHelper.java:59)
at com.mongodb.internal.connection.CommandMessage.encodeMessageBodyWithMetadata(CommandMessage.java:143)
at com.mongodb.internal.connection.RequestMessage.encode(RequestMessage.java:138)
at com.mongodb.internal.connection.CommandMessage.encode(CommandMessage.java:57)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:244)
at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99)
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:444)
at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72)
at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:200)
at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269)
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131)
at com.mongodb.operation.MixedBulkWriteOperation.executeCommand(MixedBulkWriteOperation.java:418)
at com.mongodb.operation.MixedBulkWriteOperation.executeBulkWriteBatch(MixedBulkWriteOperation.java:256)
at com.mongodb.operation.MixedBulkWriteOperation.access$700(MixedBulkWriteOperation.java:67)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:200)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:191)
at com.mongodb.operation.OperationHelper.withReleasableConnection(OperationHelper.java:419)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:191)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:67)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:193)
at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:960)
at com.mongodb.client.internal.MongoCollectionImpl.executeReplaceOne(MongoCollectionImpl.java:602)
at com.mongodb.client.internal.MongoCollectionImpl.replaceOne(MongoCollectionImpl.java:578)
at com.mongodb.client.internal.MongoCollectionImpl.replaceOne(MongoCollectionImpl.java:567)
我该怎么做才能使这项工作正常进行?谢谢。
最佳答案
有以下选项可以使其正常工作。
如果你每次都使用具体的泛型类型进行子类化,它会起作用:
public class MyClassCacheEntity extends CacheEntity<MyClass> {}
BsonDocument
+ Jackson为避免子类化,您可以尝试将任意对象存储为 BsonDocument
-s 并使用 Jackson 对它们进行序列化/反序列化:
public class CacheEntity<V> {
private String cacheId;
private BsonDocument rawValue;
// [...]
// sets the value
public void withValue(T o) {
this.rawValue = BsonDocument.parse(new ObjectMapper().writeValueAsString(o))
}
// recovers the value
public T value(Class<T> clazz) {
return new ObjectMapper().readValue(this.rawValue.toJson(), clazz);
}
}
除非您避免 withValue()
和 value()
方法的 getters/setters 约定,否则 Mongo 会开始提示相同的泛型问题。
我认为,您也可以尝试使用 org.bson.codecs.pojo.PojoCodec
执行与上述相同的操作,您可以创建或从当前的 mongo 编解码器注册表中提取它。如果你提前知道并给它真正的类,它不应该提示泛型。
关于java - MongoDB java : Perist POJO class with generic field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52197137/
我有一个 POJO 类,看起来像这样- public class CacheEntity { private String cacheId; private V value; public
我是一名优秀的程序员,十分优秀!