所以我尝试将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);
}
和blogpost类的代码
public class BlogPost {
@ObjectId
private String _id;
@ObjectId
public String get_id() {
return _id;
}
@ObjectId
public void set_id(String _id) {
this._id = _id;
}
}
我得到的错误是:
线程中的异常&#34; main&#34; java.lang.IllegalAccessError:试过 访问方法org.bson.types.ObjectId。(III)来自类的V 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完成处理
POM:
<?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>
答案 0 :(得分:3)
这很可能是依赖版本问题。您正在指定一些与您正在使用的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依赖中传递过来。