下面列出了怎么用com.fasterxml.jackson.databind.jsontype.TypeIdResolver的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public JavaType handleUnknownTypeId(DeserializationContext ctxt, JavaType baseType, String subTypeId,
TypeIdResolver idResolver, String failureMsg) throws IOException {
if (baseType.hasRawClass(RequestAspect.class)) {
log.error("Unknown AspectType found: " + subTypeId + ".");
return ReferenceType.construct(UnknownRequestAspect.class);
}
if (baseType.hasRawClass(OptionsObject.class)) {
log.error("Unknown OptionsObject found: " + subTypeId + ".");
return ReferenceType.construct(UnknownOptionsObject.class);
}
if (baseType.hasRawClass(RequestContainer.class)) {
log.error("Unknown RequestContainer found: " + subTypeId + ".");
return ReferenceType.construct(UnknownRequestContainer.class);
}
return null;
}
protected AbstractTypedJacksonModule(
Class<T> baseClass,
Function<T, String> nameResolver,
Function<String, Class<? extends T>> classResolver)
{
super(baseClass.getSimpleName() + "Module", Version.unknownVersion());
TypeIdResolver typeResolver = new InternalTypeResolver<>(nameResolver, classResolver);
addSerializer(baseClass, new InternalTypeSerializer<>(baseClass, typeResolver));
addDeserializer(baseClass, new InternalTypeDeserializer<>(baseClass, typeResolver));
}
public InternalTypeDeserializer(Class<T> baseClass, TypeIdResolver typeIdResolver)
{
super(baseClass);
this.typeDeserializer = new AsPropertyTypeDeserializer(
TypeFactory.defaultInstance().constructType(baseClass),
typeIdResolver,
TYPE_PROPERTY,
false,
null);
}
@Override
public TypeSerializer buildTypeSerializer(SerializationConfig config, JavaType baseType, Collection<NamedType> subtypes) {
//Copied this code from parent class, StdTypeResolverBuilder with same method name
TypeIdResolver idRes = this.idResolver(config, baseType, subtypes, true, false);
// have to escape "." in the middle of the "odata.type" otherwise it will be serialized to "odata": { "type":"Value"} JSON
String escapedString = this._typeProperty.replace(".", "\\.");
return new AsPropertyTypeSerializer(idRes, (BeanProperty) null, escapedString);
}
/**
* Method that can be called to obtain an instance of <code>TypeIdResolver</code> of
* specified type.
*/
public TypeIdResolver typeIdResolverInstance(Annotated annotated,
Class<? extends TypeIdResolver> resolverClass)
{
HandlerInstantiator hi = getHandlerInstantiator();
if (hi != null) {
TypeIdResolver builder = hi.typeIdResolverInstance(this, annotated, resolverClass);
if (builder != null) {
return builder;
}
}
return (TypeIdResolver) ClassUtil.createInstance(resolverClass, canOverrideAccessModifiers());
}
/**
* @since 2.8
*/
public AsPropertyTypeDeserializer(JavaType bt, TypeIdResolver idRes,
String typePropertyName, boolean typeIdVisible, JavaType defaultImpl,
As inclusion)
{
super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl);
_inclusion = inclusion;
}
/**
* @since 2.8
*/
protected TypeDeserializerBase(JavaType baseType, TypeIdResolver idRes,
String typePropertyName, boolean typeIdVisible, JavaType defaultImpl)
{
_baseType = baseType;
_idResolver = idRes;
_typePropertyName = ClassUtil.nonNullString(typePropertyName);
_typeIdVisible = typeIdVisible;
// defaults are fine, although shouldn't need much concurrency
_deserializers = new ConcurrentHashMap<String, JsonDeserializer<Object>>(16, 0.75f, 2);
_defaultImpl = defaultImpl;
_property = null;
}
/**
* Method that deserializers should call if they encounter a type id
* (for polymorphic deserialization) that cannot be resolved to an
* actual type; usually since there is no mapping defined.
* Default implementation will try to call {@link DeserializationProblemHandler#handleUnknownTypeId}
* on configured handlers, if any, to allow for recovery; if recovery does not
* succeed, will throw exception constructed with {@link #invalidTypeIdException}.
*
* @param baseType Base type from which resolution starts
* @param id Type id that could not be converted
* @param extraDesc Additional problem description to add to default exception message,
* if resolution fails.
*
* @return {@link JavaType} that id resolves to
*
* @throws IOException To indicate unrecoverable problem, if resolution cannot
* be made to work
*
* @since 2.8
*/
public JavaType handleUnknownTypeId(JavaType baseType, String id,
TypeIdResolver idResolver, String extraDesc) throws IOException
{
LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers();
while (h != null) {
// Can bail out if it's handled
JavaType type = h.value().handleUnknownTypeId(this, baseType, id, idResolver, extraDesc);
if (type != null) {
if (type.hasRawClass(Void.class)) {
return null;
}
// But ensure there's type compatibility
if (type.isTypeOrSubTypeOf(baseType.getRawClass())) {
return type;
}
throw invalidTypeIdException(baseType, id,
"problem handler tried to resolve into non-subtype: "+type);
}
h = h.next();
}
// 24-May-2016, tatu: Actually we may still not want to fail quite yet
if (!isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)) {
return null;
}
throw invalidTypeIdException(baseType, id, extraDesc);
}
/**
* @since 2.9
*/
public JavaType handleMissingTypeId(JavaType baseType,
TypeIdResolver idResolver, String extraDesc) throws IOException
{
LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers();
while (h != null) {
// Can bail out if it's handled
JavaType type = h.value().handleMissingTypeId(this, baseType, idResolver, extraDesc);
if (type != null) {
if (type.hasRawClass(Void.class)) {
return null;
}
// But ensure there's type compatibility
if (type.isTypeOrSubTypeOf(baseType.getRawClass())) {
return type;
}
throw invalidTypeIdException(baseType, null,
"problem handler tried to resolve into non-subtype: "+type);
}
h = h.next();
}
// 09-Mar-2017, tatu: We may want to consider yet another feature at some
// point to allow returning `null`... but that seems bit risky for now
// if (!isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)) {
// return null;
// }
throw missingTypeIdException(baseType, extraDesc);
}
@Test
public void typeIdResolverInstanceReturnsNull() {
// given
VirtualProperty[] customProperties = new VirtualProperty[0];
Log4j2Lookup valueResolver = new Log4j2Lookup(null);
JacksonHandlerInstantiator handlerInstantiator = createTestHandlerInstantiator(customProperties, valueResolver);
// when
TypeIdResolver result = handlerInstantiator.typeIdResolverInstance(null, null, null);
// then
Assert.assertNull(result);
}
@Override
public JavaType handleUnknownTypeId(DeserializationContext ctxt,
JavaType baseType, String subTypeId, TypeIdResolver idResolver,
String failureMsg)
throws IOException
{
return ctxt.constructType(raw);
}
@Override
public JavaType handleMissingTypeId(DeserializationContext ctxt,
JavaType baseType, TypeIdResolver idResolver,
String failureMsg)
throws IOException
{
return ctxt.constructType(raw);
}
@Override
protected TypeIdResolver idResolver(MapperConfig<?> config, JavaType baseType,
PolymorphicTypeValidator subtypeValidator, Collection<NamedType> subtypes,
boolean forSer, boolean forDeser) {
if (_idType == null) {
throw new IllegalStateException("Can not build, 'init()' not yet called");
}
return new NsTypeIdResolver(baseType, config.getTypeFactory(), basicMetaData);
}
public InternalTypeSerializer(Class<T> baseClass, TypeIdResolver typeIdResolver)
{
super(baseClass);
this.typeSerializer = new AsPropertyTypeSerializer(typeIdResolver, null, TYPE_PROPERTY);
}
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) {
return (TypeIdResolver) this.beanFactory.createBean(implClass);
}
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) {
return (TypeIdResolver) this.beanFactory.createBean(implClass);
}
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) {
return (TypeIdResolver) this.beanFactory.createBean(implClass);
}
/**
* @since 2.8
*/
public AsPropertyTypeDeserializer(JavaType bt, TypeIdResolver idRes,
String typePropertyName, boolean typeIdVisible, JavaType defaultImpl)
{
this(bt, idRes, typePropertyName, typeIdVisible, defaultImpl, As.PROPERTY);
}
/**
* @since 2.8
*/
public AsArrayTypeDeserializer(JavaType bt, TypeIdResolver idRes,
String typePropertyName, boolean typeIdVisible, JavaType defaultImpl)
{
super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl);
}
protected TypeSerializerBase(TypeIdResolver idRes, BeanProperty property)
{
_idResolver = idRes;
_property = property;
}
@Override
public TypeIdResolver getTypeIdResolver() { return _idResolver; }
/**
* @since 2.8
*/
public AsWrapperTypeDeserializer(JavaType bt, TypeIdResolver idRes,
String typePropertyName, boolean typeIdVisible, JavaType defaultImpl)
{
super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl);
}
public AsWrapperTypeSerializer(TypeIdResolver idRes, BeanProperty property) {
super(idRes, property);
}
public AsPropertyTypeSerializer(TypeIdResolver idRes, BeanProperty property, String propName)
{
super(idRes, property);
_typePropertyName = propName;
}
public AsExistingPropertyTypeSerializer(TypeIdResolver idRes,
BeanProperty property, String propName)
{
super(idRes, property, propName);
}
public AsArrayTypeSerializer(TypeIdResolver idRes, BeanProperty property) {
super(idRes, property);
}
@Override
public TypeIdResolver getTypeIdResolver() { return _idResolver; }
/**
* @since 2.8
*/
public AsExternalTypeDeserializer(JavaType bt, TypeIdResolver idRes,
String typePropertyName, boolean typeIdVisible, JavaType defaultImpl)
{
super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl);
}
public AsExternalTypeSerializer(TypeIdResolver idRes, BeanProperty property, String propName) {
super(idRes, property);
_typePropertyName = propName;
}
@Override
public TypeIdResolver getTypeIdResolver() {
return _typeSerializer.getTypeIdResolver();
}