目标
我正在尝试使用mongojack将一些数据推送到 mongo db 。我希望结果在数据库中是这样的:
{
"_id": "840617013772681266",
"messageCount": 69420,
"seedCount": 18,
"prefix": "f!",
"language": "en"
}
问题
相反,我在控制台中收到此错误。
Caused by: java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId: [840617013772681266]
at org.bson.types.ObjectId.parseHexString(ObjectId.java:390)
at org.bson.types.ObjectId.<init>(ObjectId.java:193)
at org.mongojack.internal.ObjectIdSerializer.serialiseObject(ObjectIdSerializer.java:66)
at org.mongojack.internal.ObjectIdSerializer.serialize(ObjectIdSerializer.java:49)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)
... 59 more
代码
这是我尝试在数据库中创建新公会时调用的代码:
public static Guild getGuild(String id) throws ExecutionException {
return cache.get(id);
}
cache
如下(加载被执行):
private static LoadingCache<String, Guild> cache = CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.build(
new CacheLoader<>() {
@Override
public Guild load(@NotNull String id) {
return findGuild(id).orElseGet(() -> new Guild(id, "f!"));
}
});
首先调用的findGuild
方法:
public static Optional<Guild> findGuild(String id) {
return Optional.ofNullable(guildCollection.find()
.filter(Filters.eq("_id", id)).first());
}
最后是Guild
文件。
@Getter
@Setter
public class Guild implements Model {
public Guild(String id, String prefix) {
this.id = id;
this.prefix = prefix;
}
public Guild() {
}
private String id;
/*
If a Discord guild sent 1,000,000,000 messages per second,
it would take roughly 292471 years to reach the long primitive limit.
*/
private long messageCount;
private long seedCount;
// The default language is specified in BotValues.java's bot.yaml.
private String language;
private String prefix;
@ObjectId
@JsonProperty("_id")
public String getId() {
return id;
}
@ObjectId
@JsonProperty("_id")
public void setId(String id) {
this.id = id;
}
}
我试过的
我尝试了多种方法,例如Long.toHexString(Long.parseLong(id))
说实话,我不完全理解错误,在查看文档后,我留下的问题多于答案。