Skip to content

AnnotationIntrospector

Tatu Saloranta edited this page Jul 25, 2020 · 1 revision

Jackson Glossary: AnnotationIntrospector

  • Package: com.fasterxml.jackson.databind.introspect
  • Jar: jackson-databind

Role

Annotation introspectors are amongst the most powerful entities that guide configuration of all aspects of serialization and deserialization. AnnotationIntrospector defines an API that various SerializerFactory and DeserializerFactory implementations use to find out what annotation overrides to apply; but without having to know any details of how annotations are used. Introspector is then responsible for translating physical annotations (like Jackson and JAXB annotations) into logical meaning ("call this property 'BAR' instead of what bean naming convention would say")

Instances need to be fully thread-safe, which usually means they are stateless.

In addition to using individual introspectors it is also possible (and common) to use multiple introspectors, to allow use of multiple sets of annotations. This is achieved by using AnnotationIntrospector.Pair helper class; it defines primary and secondary introspectors to use (with matching precedence); and with chaining can be used to use unlimited number of introspectors.

Usage

Introspectors are not directly used, but they may need to be instantiated and registered with ObjectMapper and related objects.

In 2.x, you can use an AnnotationIntrospector via a custom Module.

In 1.x, if you want to use both Jackson and JAXB annotations, you can this by:

ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector intr = new AnnotationIntrospector.Pair(
  new JaxbAnnotationIntrospector(),
  new JacksonAnnotationIntrospector()
);
// usually we use same introspector(s) for both serialization and deserialization:
mapper.getDeserializationConfig().setAnnotationIntrospector(intr);
mapper.getSerializationConfig().setAnnotationIntrospector(intr);

A more advanced way to use Annotation introspector is to define custom introspector that does not necessarily read any annotations, but allows configuration overrides using methods defined. This can be used, for example, to do dynamic renaming of property names (change camel-casing to title casing); or to implement alternative property introspection strategies.

Known sub-classes


Back to Jackson Term Glossary