3

So I am trying use MongoJack together with MongoDB and jackson to access Data from MongoDB and turn it into POJO's. I seem to be struggling even though I am doing it in the simplest possible manner and have basically just copy pasted the mongojack tutorial at this point (http://mongojack.org )

The code where I am trying to get the object:

    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);


}

and the code for the blogpost class

public class BlogPost {

    @ObjectId
    private String _id;

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

the error I am getting is:

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

Have tried tons of different variations now and adding to the database etc works fine, just not creating an object from MongoDB.

Edit:

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)

Process finished with exit code 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>

4
  • These kinds of problems are often related to incompatible versions of the different frameworks. You should post your build tool setup (e.g. your pom.xml if you use maven). Also, please post the complete stacktrace. Commented Jun 30, 2015 at 19:52
  • @K Erlandsson like so? hmm Not sure how to @reply to someone with a space in their name :( Commented Jun 30, 2015 at 20:03
  • Ok so updating the MongoJack to the newest version seems to have fixed the problem. Sort of feel like an idiot for spending so much time on this, but Oh well I guess Lesson learned. Thanks a lot @KErl Commented Jun 30, 2015 at 20:08
  • Even if that might have fixed this issue you should do as I write in my answer and remove some dependencies to ensure totally compatible versions. Commented Jun 30, 2015 at 20:11

1 Answer 1

3

This is most probably a dependency version problem. You are specifying some libraries that are incompatible with the version of MongoJack you are using. You should remove the following 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>

And let them come transitively from the MongoJack dependency.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.