6

I have following JSON

{
  "known-name": "Zevs",
  "approximate-age": 320
}

And binding class

public class GodBinding {

  @JsonProperty("known-name")
  public String name;

  @JsonProperty("approximate-age")
  public int age;

  // constructors
  // getters & setters
}

And followng maven dependencies 2.23.2 2.5.4

 <dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
        <scope>provided</scope>
    </dependency>

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

If I post such json then I have unexpected result with null's.

GodBinding [name=null, age=0]

If I use @JsonProperty without names and send JSON where property names equal field names

{
  "name": "Zevs",
  "age": 320
}

then it's working fine

GodBinding [name=Zevs, age=320]

If somebody know, how to make @JsonProperty("name") on fields working correctly?

cane
  • 892
  • 1
  • 10
  • 16
  • @JsonProperty("anyname") annotation ignore any given name and tryies to bind data by field name only. – cane Oct 18 '16 at 14:08
  • None of the provided solutions helped me. Using jackson version 2.9.4. What else could be the reason for the annotation not working? – User3518958 Aug 07 '19 at 11:27

3 Answers3

6

This is often caused when the annotations of Jackson is of Jackson 1, but you want to use Jackson 2, as mentioned in many other questions.

In my case, in the project I have another dependency which was imported wrongly:

import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;

when I create the ObjectMapper. I guess testcontainers uses its own ObjectMapper as its dependency and exposed it incorrectly; actually it is an older version. Not sure which is.

I change it to

import com.fasterxml.jackson.databind.ObjectMapper;

and all works now. This is what I call "first level dependency", not "dependency of dependency". In my gradle file it is version 2.3.

I mention this because:

  • I see other questions only mentioning the confusion between Jackson 1 and 2, not this of testcontainer. We just must ignore those not of fasterxml.jackson.

  • Pay attention of the version not only of @JsonProperty, etc, but also the version of Jackson you use when importing ObjectMapper and DeserializationFeature.

WesternGun
  • 11,303
  • 6
  • 88
  • 157
0

You should add this to your POM

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

you can see an example here

Paul
  • 571
  • 3
  • 17
Efren Narvaez
  • 163
  • 11
  • jackson-databind already were because of jackson-jaxrs-json-provider. – cane Oct 18 '16 at 14:04
  • For given example You have equal json property name and object field name. name or lastName for example. That is why - no problems. – cane Oct 18 '16 at 14:21
  • No, if jackson is exposed as the dependency of some other dependency, it may be a wrong version and it will not work probably, which is my case. We should only use `fasterxml.jackson`. See my answer. – WesternGun Nov 08 '18 at 11:10
0

Just for reference, I fixed this same issue by importing explicitly the com.fasterxml.jackson.core:jackson-databind:2.9.9 instead of using the ObjectMapper indirectly imported by other packages.


Hints:

  1. import the right dependency;
  2. import from the right dependency;
Hearen
  • 7,420
  • 4
  • 53
  • 63