I have a problem with Jackson Mixon and inheritance. I have two target classes: parent and child. For these two target classes, I defined two MixIn classes (interfaces) with no inheritance relationship with each other. I also tested with one MixIn interface extending the others, but there was no difference in the results. When Jackson serializes the parent class, he uses the correctly defined mixin to configure serialization, and everything works well. However, when Jackson serializes the child class, he will use the mixin parent class definitions to serialize the properties that exist in both the parent and child class. He then uses the definitions of the child mixin class to serialize the properties defined in the child class, but not in the parent class. Now it's probablyhas something to do with comparing base classes or implementing interfaces in Jackson.
Now the question is, is there a way I could instruct Jackson to use only mixin definitions for a child class when serializing objects of a child class? And yes, I would like to leave both mixin definitions in two separate use cases, so just removing the mixin mapping of the parent class will not solve my problem.
Sample code and expected and actual output JSON below.
Environment: Jackson version 2.1.4 Tomcat version 7.0.34.0
The target classes and interfaces that they implement are:
public interface TestI {
public String getName();
}
public interface TestExtendI extends TestI {
public Integer getAge();
}
public class Test implements TestI {
String name;
public Test(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
}
public class TestExtend extends Test implements TestExtendI {
private Integer age;
public TestExtend(String name) {
super(name);
}
public TestExtend(String name, Integer age) {
super(name);
this.age = age;
}
@Override
public Integer getAge() {
return age;
}
}
Mixin Definitions
public interface TestMixIn {
@JsonProperty("base-name")
public String getName();
}
public interface TestExtendMixIn {
@JsonProperty("ext-name")
public String getName();
@JsonProperty("ext-age")
public Integer getAge();
}
If both mixes are added to the mapper, the output is JSON:
{
"base-name": "5", // from parent class mixin definition
"ext-age": 50 // from child class mixin defition
}
With mixin for TestI.class, commented that everything works as expected, and JSON output (this is what I would like to achieve):
{
"ext-name": "5", // from child class mixin defition
"ext-age": 50 // from child class mixin defition
}
Object Configurator Configuration
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonObjectMapper implements ContextResolver<ObjectMapper> {
private ObjectMapper mapper;
public JacksonObjectMapper() {
mapper = new ObjectMapper();
mapper.addMixInAnnotations(TestI.class, TestMixIn.class);
mapper.addMixInAnnotations(TestExtendI.class, TestExtendMixIn.class);
}
public ObjectMapper getContext(Class<?> type) {
return this.mapper;
}
}
REST api for request / response processing
@Path("api/test/{id}")
public class TestRestApi {
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public TestI getTest(@PathParam("id") String id) {
TestI ret = new TestExtend(id, 50);
return ret;
}
}
Decision
pgelinas , , , "" mixin . TestExtendI:
public interface TestExtendI extends TestI {
public Integer getAge();
@Override
public String getName();
}
. , , .