4

The Short Version:

I am using Jersey 1.17 and Jackson 2.1.4, but Jersey is ignoring every single Jackson-annotation that I use. Why!? Has anybody encountered the same problem?

The Long Version:

I have a RESTful Web Service that was using Jersey 1.17 and Jackson 1.9.2 and I was using the @JsonManagedReference and @JsonBackReference annotations to solve the problem with bidirectional relationships and everything was working fine.

The other day I saw the 2.1.4 version of Jackson that included the @JsonIdentityInfo annotation which seemed like a great solution for my problems so I changed the jar files to Jackson 2.1.4. However, the promising @JsonIdentityInfo annotation was not working and Jersey was ignoring it completely, and after a bit of debugging and changing the code, I found out that even the previous @JsonManagedReference and @JsonBackReference annotations are being ignored by Jersey. So it seems that Jersey is completely ignoring the Jackson 2.1.4 annotations. Why!? Has anybody encountered the same problem?

bbkglb
  • 769
  • 1
  • 7
  • 16
  • Obvious question - Are using the POJOMapping feature? Jackson is used only when that is enabled. – calvinkrishy Mar 15 '13 at 15:57
  • @calvinkrishy Yes I am using that, I have mentioned above that the 1.9.2 version of Jackson was working flawlessly... – bbkglb Mar 15 '13 at 17:07
  • Are you using Maven to build your application? – Perception Mar 18 '13 at 12:00
  • @Perception No. I have solved the problem with some other ways using the old 1.9.2 version though, but I'm still curious to find the reason why the new 2.1.4 version won't work with jersey... :-/ – bbkglb Mar 19 '13 at 08:16
  • 1
    Make sure that the JacksonJsonProvider you register with Jersey is not the old version. – HiJon89 Mar 20 '13 at 14:02
  • You might want to look at [my question and answer](http://stackoverflow.com/a/15948543/150992) ... I had a similar problem. In my case, I needed to turn the POJOMapping feature off in order to get the Jackson 2.x annotations recognized. – Eyal Oct 23 '13 at 17:36

2 Answers2

3

As stated in http://wiki.fasterxml.com/JacksonAnnotations:

Important note: Jackson 1.x and 2.x annotations live in different Java and Maven packages: see Jackson 2.0 for full explanation:

  • 1.x annotations are in Java package org.codehaus.jackson.annotate, in Jackson core jar
  • 2.x annotations are in Java package com.fasterxml.jackson.annotation, in "jackson-databind" jar.

So could be an "import-related" problem. Another reason (as already stated by @HiJon89) is to use the right JacksonJsonProvider version. For version 2.x it's automatically registered while inserting the "jackson-jaxrs-json-provider" combined with "jersey-core".

My currently working update to jackson 2.1.4 (from a 1.9.2 jersey required dependency) was:

  • remove jersey-json dependency
  • add jersey-core and jackson-jaxrs-json-provider

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>${jackson-jaxrs-json-provider.version}</version>
    </dependency>
    

The latter jar has a file "META-INF/services/javax.ws.rs.ext.MessageBodyWriter" that tell jersey what class act as a provider.

So I've no other advise than to double-check that you don't have the old jars anymore in your classpath.

SQB
  • 3,926
  • 2
  • 28
  • 49
Fabio Bonfante
  • 5,128
  • 1
  • 32
  • 37
  • Unfortunately no, I have imported the correct com.fasterxml.jackson.annotation package and that's not the issue here. Thanks though. – bbkglb Apr 05 '13 at 19:19
0

In my case jackson annotations were ignored because of proguard

Lukas Hanacek
  • 1,364
  • 14
  • 25