3

I'm using the ObjectMapper class from the fasterxml package (com.fasterxml.jackson.databind.ObjectMapper) to serialize some POJOs.

The problem I'm facing is that all the annotations in the POJOs are from the older codehaus library. The fasterxml ObjectMapper is not recognizing the codehaus jackson annotations.
One possible solution is to update the annotations in the POJO to fasterxml, but the POJOs are provided by a third party, hence I cannot modify it.

How can I solve this problem?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Raj
  • 4,342
  • 9
  • 40
  • 45

2 Answers2

3

You can provide your own AnnotationIntrospector to process old annotation.

ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new MyAnnotationIntrospector());

You can also checkout the jackson-legacy-introspector listed on the jackson github. It's an existing implementation of AnnotationIntrospector for old annotations.

JEY
  • 6,973
  • 1
  • 36
  • 51
-1

If you can use workaround with inheritance

// Original class doesn't need to be modified
class Customer {
     @org.codehaus.jackson.annotate.JsonProperty("first_name")
     String firstName;
}

class CustomerWrapper extends Customer {
     @com.fasterxml.jackson.annotation.JsonProperty("first_name")
     String firstName;
}

And in code use CustomerWrapper class which will be serialized correctly

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MarekM
  • 1,426
  • 17
  • 14