我知道自最初的问题以来已经过去了3年,但是仍然不支持点嵌套属性,也许这会帮助某人。我最终创建了一个类,以便我们可以按预期使用点语法。只需添加到任何带有嵌套鉴别器的类中,海报的原始尝试就会起作用:NestedTypeResolver
@JsonTypeResolver(NestedTypeResolver.class)
/**
* My ActivityDisplayModel Abstract Class
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type.id")
@JsonSubTypes({
@JsonSubTypes.Type(value = MealDisplayModel.class, name = "MEAL"),
@JsonSubTypes.Type(value = EntertainmentDisplayModel.class, name = "ENTERTAINMENT")
})
@JsonTypeResolver(NestedTypeResolver.class)
public abstract class ActivityDisplayModel {
NestedTypeResolver:
/**
* Allows using nested "dot" dyntax for type discriminators. To use, annotate class with @JsonTypeResolver(NestedTypeResolver.class)
*/
public class NestedTypeResolver extends StdTypeResolverBuilder {
@Override
public TypeDeserializer buildTypeDeserializer(DeserializationConfig config, JavaType baseType,
Collection<NamedType> subtypes) {
//Copied this code from parent class, StdTypeResolverBuilder with same method name
TypeIdResolver idRes = idResolver(config, baseType, subtypes, false, true);
return new NestedTypeDeserializer(baseType, idRes, _typeProperty, _typeIdVisible,
null, _includeAs);
}
}
所有繁重的工作都在这里完成,NestedTypeDeserializer:
/**
* Heavy work to support {@link NestedTypeResolver}
*/
public class NestedTypeDeserializer extends AsPropertyTypeDeserializer {
private static final Logger LOGGER = LoggerFactory.getLogger(NestedTypeDeserializer.class);
public NestedTypeDeserializer(JavaType bt,
TypeIdResolver idRes, String typePropertyName, boolean typeIdVisible,
JavaType defaultImpl) {
super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl);
}
public NestedTypeDeserializer(JavaType bt, TypeIdResolver idRes, String typePropertyName, boolean typeIdVisible,
JavaType defaultImpl, JsonTypeInfo.As inclusion) {
super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl, inclusion);
}
public NestedTypeDeserializer(AsPropertyTypeDeserializer src, BeanProperty property) {
super(src, property);
}
@Override
public TypeDeserializer forProperty(BeanProperty prop) {
return (prop == _property) ? this : new NestedTypeDeserializer(this, prop);
}
@Override
public Object deserializeTypedFromObject(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonNode originalNode = p.readValueAsTree();
JsonNode node = originalNode;
//_typePropertyName is the dot separated value of "property" in @JsonTypeInfo
LOGGER.debug("Searching for type discriminator [{}]...", _typePropertyName);
for (String property : _typePropertyName.split("\\.")) { //traverse down any nested properties
JsonNode nestedProp = node.get(property);
if (nestedProp == null) {
ctxt.reportWrongTokenException(p, JsonToken.FIELD_NAME,
"missing property '" + _typePropertyName + "' that is to contain type id (for class "
+ baseTypeName() + ")");
return null;
}
node = nestedProp;
}
LOGGER.debug("Found [{}] with value [{}]", _typePropertyName, node.asText());
JsonDeserializer<Object> deser = _findDeserializer(ctxt, "" + node.asText());
//Since JsonParser is a forward-only operation and finding the "type" discriminator advanced the pointer, we need to reset it
//Got clues from https://www.dilipkumarg.com/dynamic-polymorphic-type-handling-jackson/
JsonParser jsonParser = new TreeTraversingParser(originalNode, p.getCodec());
if (jsonParser.getCurrentToken() == null) {
jsonParser.nextToken();
}
return deser.deserialize(jsonParser, ctxt);
}
}
免责声明:我们已经在Jackson 2.8.10上使用了一个月,并且没有遇到任何问题,但是我们必须深入研究Jackson源代码杂草才能完成它,所以YMMV。希望杰克逊有一天会允许这种开箱即用,这样我们就不需要这些解决方法。