Skip to content
Advertisement

Jackson – Custom TypeId Resolver

I’ve been using a custom typeId resolver for one of my classes, so far I’ve been leaning on the annotation support:

JavaScript

But now I need to customize the creation of the type resolver by passing some other dependencies to it via constructor or setters, and since Jackson is the one who instantiate it I can’t find a way around it.

Is there a way to configure the ObjectMapper to use a TypeIdResolver instead of relying on annotations?

Regards

Advertisement

Answer

So you have two options:

1) If you’re set on using the @JsonTypeIdResolver your stuck using static state in your TypeIdResolver. This probably isn’t what you want.

The default JacksonAnnotationIntrospector will try to create an instance of the type you provide using JsonTypeIdResolver per its default constructor. There is currently no way to configure it to do otherwise.

JavaScript

2) Is create a module to handle deserialization of your type and subtypes.

JavaScript

For more detailed examples, see Jackson documentation How-To: Custom Deserializers.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement