gpt4 book ai didi

java - 不兼容的类型 : inference variable T has incompatible bounds equality constraints: capture#1 of ? 扩展了 java.lang.Object

转载 作者:IT老高 更新时间:2023-10-28 12:31:20 44 4
gpt4 key购买 nike

我正在尝试连接以运行查询以获取 MongoDB 中的所有记录,然后将记录转换为引用对象类型的列表,我将其作为调用类的泛型。代码运行良好并在 Eclipse 中实现了预期的结果,但在 maven build 期间出现编译错误,maven 和 eclipse 都引用相同的 JDK(1.8)。有人可以帮我解决这个问题吗

public class MongoPersistenceImpl<T> {

MongoDatabase database=(MongoDatabase)MongoConnectImpl.getInstance().getConnection();

public List<T> getAll(T modelObject){
MongoCollection<Document> collection=database.getCollection(MongoConnectImpl.MONGO_COLLECTION);
List<T> reportList=new ArrayList<>();
Gson gson=new Gson();
MongoCursor<Document> cursor = collection.find().iterator();
try {
while (cursor.hasNext()) {
T report=gson.fromJson(cursor.next().toJson(),modelObject.getClass());
reportList.add(report);
}
return reportList;
}catch(Exception e){
CatsLogger.printLogs(3, "30016", e, MongoPersistenceImpl.class,new Object[]{"get all"} );
return null;
} finally {
cursor.close();
}
}

}

日志:-

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] incompatible types: inference variable T has incompatible bounds
equality constraints: capture#1 of ? extends java.lang.Object
upper bounds: T,java.lang.Object

关于复制同一存在的完整消息:-

enter image description here

更新:显式类型转换对象变量有效,但我仍然需要了解如何?

  public List<T> getAll(T modelObject){
MongoCollection<Document> collection=database.getCollection(MongoConnectImpl.MONGO_COLLECTION);

List<T> reportList=new ArrayList<T>();
Gson gson=new Gson();
MongoCursor<Document> cursor = collection.find().iterator();
try {
while (cursor.hasNext()) {
Object rep=gson.fromJson(cursor.next().toJson(),modelObject.getClass());
T report=(T)rep;//explicit type cast
reportList.add(report);
}
return reportList;
}catch(Exception e){
CatsLogger.printLogs(3, "30016", e, MongoPersistenceImpl.class,new Object[]{"get all"} );
return null;
} finally {
cursor.close();
}
}

最佳答案

当您尝试将对象转换为 report 的特定 Type 时,请尝试更改

T report = gson.fromJson(cursor.next().toJson(), modelObject.getClass());

T report = gson.fromJson(cursor.next().toJson(), (java.lang.reflect.Type) modelObject.getClass());

关于java - 不兼容的类型 : inference variable T has incompatible bounds equality constraints: capture#1 of ? 扩展了 java.lang.Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46460874/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com