69

I am new to Jackson and I was writing some code for practice. I found out that the new version of Jackson library can be found on Fasterxml: Jackson, so I added the below dependencies to my Maven POM-file:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.2.2</version>
</dependency>
 

I was expecting that I can use the ObjectMapper directly, however after spending a lot of time I found out that to use the ObjectMapper I have to add the old libraries below:

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.2</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.2</version>
</dependency>

I am a bit confused. Could someone please tell me why is that?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Hossein
  • 40,161
  • 57
  • 141
  • 175
  • Do you have issues with package names? As far as i see com.fasterxml.jackson.databind.ObjectMapper is part of jackson-databind-2.2.2.jar – efan Aug 25 '13 at 13:50
  • The problem is that as soon as I remove the last two dependencies. I cannot compile my code because of ObjectMapper. what do u mean by "package names" ? thx – Hossein Aug 25 '13 at 13:55
  • 8
    For me this looks like you code is using org.codehaus.jackson.map.ObjectMapper instead of com.fasterxml.jackson.databind.ObjectMapper and because of this could not find class and could not compile. – efan Aug 25 '13 at 13:58
  • Could you show to us problematic code? – Michał Ziober Aug 26 '13 at 22:39

5 Answers5

61
<properties>
  <!-- Use the latest version whenever possible. -->
  <jackson.version>2.4.4</jackson.version>
</properties>
<dependencies>
   <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
  </dependency>
</dependencies>

you have a ObjectMapper (from Jackson Databind package) handy. if so, you can do:

JsonFactory factory = objectMapper.getFactory();

Source: https://github.com/FasterXML/jackson-core

So, the 3 "fasterxml" dependencies which you already have in u'r pom are enough for ObjectMapper as it includes jackson-databind.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
ASD
  • 629
  • 5
  • 6
11

No, you can simply use com.fasterxml.jackson.databind.ObjectMapper. Most likely you forgot to fix your import-statements, delete all references to codehaus and you're golden.

specializt
  • 1,913
  • 15
  • 26
4

The package names in Jackson 2.x got changed to com.fasterxml1 from org.codehaus2. So if you just need ObjectMapper, I think Jackson 1.X can satisfy with your needs.

chenrui
  • 8,910
  • 3
  • 33
  • 43
2

I spent few hours on this.

Even if I had the right dependency the problem was fixed only after I deleted the com.fasterxml.jackson folder in the .m2 repository under C:\Users\username.m2 and updated the project

QGA
  • 3,114
  • 7
  • 39
  • 62
  • It happens. The problem is that unless we specify an artifact version using [] notation, what specify is a "suggestion". Maven will try to get that version, but will opt for something difference if there's something in your transitive dependencies that require it. I nuke my .m2 regularly just to avoid these "surprises." – luis.espinal May 02 '19 at 17:31
1

Apart from fixing the imports, do a fresh maven clean compile -U. Note the -U option, that brings in new dependencies which sometimes the editor has hard time with. Let the compilation fail due to un-imported classes, but at least you have an option to import them after the maven command.

Just doing Maven->Reimport from Intellij did not work for me.

nilesh
  • 14,131
  • 7
  • 65
  • 79