Failed to upgrade to Jackson 2.1.4, Jersey, ignoring annotations

Short version:

I use Jersey 1.17 and Jackson 2.1.4, but Jersey ignores every Jackson annotation I use. What for!? Has anyone encountered the same problem?

Long version:

I have a RESTful web service using Jersey 1.17 and Jackson 1.9.2, and I used the @JsonManagedReference and @JsonBackReference annotations to solve the bi-directional relationship problem and everything works fine.

The other day, I saw a version of Jackson 2.1.4 that contained the @JsonIdentityInfo annotation, which seemed like a great solution to my problems, so I changed the jar files to Jackson 2.1.4. However, the promising @JsonIdentityInfo annotation did not work, and Jersey completely ignored it, and after some debugging and code changes, I found out that even the previous @JsonManagedReference and @JsonBackReference annotations are ignored by Jersey. Therefore, it seems that Jersey completely ignores Jackson 2.1.4 annotations. What for!? Has anyone encountered the same problem?

+3
source share
2 answers

As stated at 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 a full explanation:

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

So there may be a problem related to import. Another reason (as mentioned in @ HiJon89) is to use the correct version of JacksonJsonProvider. For version 2.x, it is automatically registered when you insert jackson-jaxrs-json-provider in combination with jersey-core.

My current working update for jackson 2.1.4 (from the 1.9.2st 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 last bank has the file "META-INF / services / javax.ws.rs.ext.MessageBodyWriter", which indicates which class acts as a provider.

So I have no other advice than to double-check that you no longer have old cans in your class path.

+3
source

In my case, Jackson's annotations were ignored due to proguard

0
source

Source: https://habr.com/ru/post/1501338/


All Articles