gpt4 book ai didi

java.lang.IllegalAccessError 当试图从 MongoDB 制作 POJO 时?

转载 作者:可可西里 更新时间:2023-11-01 10:03:35 25 4
gpt4 key购买 nike

所以我正在尝试将 MongoJack 与 MongoDB 和 jackson 一起使用,以从 MongoDB 访问数据并将其转换为 POJO。即使我以最简单的方式进行操作,并且此时基本上只是复制粘贴了 mongojack 教程,我似乎仍在挣扎 (http://mongojack.org)

我试图获取对象的代码:

    public static void main(String[] args) {

MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017)));
DB db1 = mongoClient.getDB("test1");
DBCollection coll = db1.getCollection("things1");
JacksonDBCollection<BlogPost, String> Jcoll =
JacksonDBCollection.wrap(coll,
BlogPost.class,
String.class);


BlogPost blogPost = new BlogPost();
WriteResult<BlogPost, String> result = Jcoll.insert(blogPost);


String savedId = result.getSavedId();

System.out.println(savedId);


BlogPost test3 = Jcoll.findOneById(savedId);


}

和博客文章类的代码

public class BlogPost {

@ObjectId
private String _id;

@ObjectId
public String get_id() {
return _id;
}
@ObjectId
public void set_id(String _id) {
this._id = _id;
}
}

我得到的错误是:

Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.bson.types.ObjectId.(III)V from class org.mongojack.internal.stream.ObjectIdConvertor.

现在已经尝试了很多不同的变体并且添加到数据库等工作正常,只是没有从 MongoDB 创建对象。

编辑:

Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.bson.types.ObjectId.<init>(III)V from class org.mongojack.internal.stream.ObjectIdConvertor
at org.mongojack.internal.stream.ObjectIdConvertor.convert(ObjectIdConvertor.java:30)
at org.mongojack.internal.stream.DBDecoderBsonParser.getEmbeddedObject(DBDecoderBsonParser.java:70)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:36)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:2965)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1626)
at org.mongojack.internal.stream.JacksonDBDecoder.decode(JacksonDBDecoder.java:77)
at org.mongojack.internal.stream.JacksonDBDecoder.decode(JacksonDBDecoder.java:64)
at com.mongodb.DBDecoderAdapter.decode(DBDecoderAdapter.java:49)
at com.mongodb.DBDecoderAdapter.decode(DBDecoderAdapter.java:29)
at com.mongodb.connection.ReplyMessage.<init>(ReplyMessage.java:57)
at com.mongodb.connection.QueryProtocol.receiveMessage(QueryProtocol.java:290)
at com.mongodb.connection.QueryProtocol.execute(QueryProtocol.java:229)
at com.mongodb.connection.QueryProtocol.execute(QueryProtocol.java:38)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:155)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:219)
at com.mongodb.connection.DefaultServerConnection.query(DefaultServerConnection.java:166)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:397)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:394)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:195)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:168)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:394)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:57)
at com.mongodb.Mongo.execute(Mongo.java:738)
at com.mongodb.Mongo$2.execute(Mongo.java:725)
at com.mongodb.DBCursor.initializeCursor(DBCursor.java:815)
at com.mongodb.DBCursor.hasNext(DBCursor.java:149)
at org.mongojack.DBCursor.hasNext(DBCursor.java:348)
at org.mongojack.JacksonDBCollection.findOne(JacksonDBCollection.java:1364)
at org.mongojack.JacksonDBCollection.findOne(JacksonDBCollection.java:1334)
at org.mongojack.JacksonDBCollection.findOneById(JacksonDBCollection.java:1282)
at org.mongojack.JacksonDBCollection.findOneById(JacksonDBCollection.java:1269)
at com.tapdaq.restapi.MongoConfiguration.main(MongoConfiguration.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

进程结束,退出代码为 1

任务:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.tapdaq.restapi</groupId>
<artifactId>restapi</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<dropwizard.version>0.8.1</dropwizard.version>
</properties>

<build><plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.tapdaq.restapi.HelloWorldApplication</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

</plugins></build>
<dependencies>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>org.mongojack</groupId>
<artifactId>mongojack</artifactId>
<version>2.1.0</version>
</dependency>


<dependency>

<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
</dependencies>

最佳答案

这很可能是依赖版本问题。您正在指定一些与您正在使用的 MongoJack 版本不兼容的库。您应该删除以下依赖项:

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>2.13.2</version>
</dependency>

并让它们从 MongoJack 依赖项传递过来。

关于java.lang.IllegalAccessError 当试图从 MongoDB 制作 POJO 时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31147222/

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