Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson ObjectMapper() constructor throws NoSuchMethod

I'm using Jackson sample code to deserialize a POJO:

ObjectMapper m = new ObjectMapper();

This line throws a NoSuchMethodError:

Exception in thread "main" java.lang.NoSuchMethodError: org.codehaus.jackson.type.JavaType.<init>(Ljava/lang/Class;)V
    at org.codehaus.jackson.map.type.TypeBase.<init>(TypeBase.java:15)
    at org.codehaus.jackson.map.type.SimpleType.<init>(SimpleType.java:45)
    at org.codehaus.jackson.map.type.SimpleType.<init>(SimpleType.java:40)
    at org.codehaus.jackson.map.type.TypeBindings.<clinit>(TypeBindings.java:18)
    at org.codehaus.jackson.map.type.TypeFactory._fromType(TypeFactory.java:525)
    at org.codehaus.jackson.map.type.TypeFactory.type(TypeFactory.java:61)
    at org.codehaus.jackson.map.ObjectMapper.<clinit>(ObjectMapper.java:179)
    at com.me.util.ctrl.BillingJobStatus.fromJson(BillingJobStatus.java:37)

I don't get it

like image 867
Mojo Avatar asked Feb 23 '11 17:02

Mojo


People also ask

What is the use of Jackson ObjectMapper?

ObjectMapper is the main actor class of Jackson library. ObjectMapper class ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions.

Should I declare Jackson's ObjectMapper as a static field?

Yes, that is safe and recommended.

How does ObjectMapper readValue work?

The simple readValue API of the ObjectMapper is a good entry point. We can use it to parse or deserialize JSON content into a Java object. Also, on the writing side, we can use the writeValue API to serialize any Java object as JSON output.

Is Fasterxml ObjectMapper thread-safe?

Mapper instances are fully thread-safe provided that ALL configuration of the instance occurs before ANY read or write calls.


3 Answers

I'm guessing your Jackson JARs are out of sync. The JavaType class is in the jackson-core JAR, and the ObjectMapper class is in jackson-mapper.

Make sure these are both of the same version.

like image 188
skaffman Avatar answered Oct 16 '22 08:10

skaffman


I had this same problem. The core jar was 1.7.1 while the mapper was 1.8.1. Note: To fix this for maven I added an exclusion and pulled down the proper version.

        <exclusions>
            <exclusion>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
            </exclusion>
        </exclusions>
like image 32
Jeff Avatar answered Oct 16 '22 06:10

Jeff


The trick here is to exclude jackson from the dependencies that use it.

To check which dependencies import it, you can use the following maven command:

mvn dependency:tree -Dincludes=org.codehaus.jackson

like image 5
wild_nothing Avatar answered Oct 16 '22 06:10

wild_nothing