- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
所以我正在尝试将 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/
我是 C++ 的新手,我在使用这段代码时遇到了问题: string output_date(int day, int month, int year){ string date; if
所以我这样做了 tar cvzf test.zip FP 为了创建目录 FP 的 zip 但是,它会列出 zip 中的目录 FP/ FP/php/ FP/php/pdf/ FP/php/docs/ F
我正在尝试在 Swift、Xcode 7.3(所以是 Swift 2.2)中创建一个通用类,但我似乎无法让它通过编译器: protocol Struct1Protocol { } struct Str
我的测试用例是这样的: class FooTest extends PHPUnit_Framework_TestCase { /** @covers MyClass::bar */ f
我正在尝试将brew install wine作为使electron-builder工作的一步。但是我所能得到的只是以下响应: ==> Installing dependencies for wine
我这样做: string[,] string1 = {{"one", "0"},{"Two", "5"},{"Three","1"}}; int b = 0; for(int i = 0; i <=
我正在尝试使用 SetWindowsHookEx 键盘 Hook Notepad.exe。 如您所见,工作线程正在将其 ASCII 代码(即 wParam)发送到指定的服务器。 UINT WINAPI
我正在尝试将 ListView 实现到我的 Fragment 中,但无论我尝试什么,我都会得到一个 NullPointerException。我检查对象是否为 null 并记录是否为 null,看起来
我尝试在一行中对齐两个 div。使用 float left 属性,一切顺利。但是当我在 div 中使用图像时,它开始产生问题。 所以这是我的示例代码:- Some headi
我目前正在使用此代码来获取图像的灰度图像表示并以 (512, 370, 1) 的格式表示它大批。 img_instance = cv2.imread(df.iloc[i][x_col]) / 255.
总结 我正在创建一个简单的应用程序,它允许用户选择一个包含顶级窗口的进程。用户首先键入 native DLL(而非托管 DLL)的路径。然后用户键入将在 Hook 过程中调用的方法的名称。该方法不得返
我是一名优秀的程序员,十分优秀!