Using Jackson deduction without modifying POJO classes

927 Views Asked by At

Jackson has a great feature that allows performing polymorphic deserialization without changing the JSON structure. Here's the example. However, I can't use it since it requires adding annotations like @JsonTypeInfo(use=Id.DEDUCTION) and @JsonSubTypes({ ... }) to classes that, in my case, are generated.

I am looking for ways to achieve the same behavior without changing the source code of POJOs. For example, are there any ways to do it by configuring the object mapper itself?

1

There are 1 best solutions below

0
On

ObjectMapper MixIns seem to be exactly what you're looking for.

Create a class stub like:

@JsonTypeInfo(use=Id.DEDUCTION)
@JsonSubTypes({ ... })
abstract class MyPojoMixIns {
}

Then register it with your ObjectMapper like:

objectMapper.addMixIn(MyPojo.class, MyPojoMixIns.class);