On Sun, Mar 17, 2019 at 11:12 PM Yajian Shi <[email protected]> wrote: > > i have a interface like this: > 在此输入代码... > public interface Enumerable { > // some methods > } > > and i make my enums implements it , like this : > 在此输入代码... > > public enum UserStatusEnum implements Enumerable { > // some fields & some methods > } > > > > 在此输入代码... > > // this my customer deserializer > > public class EnumDeserializer<E extends Enumerable> extends > StdDeserializer<E> { > > private Class<E> enumType; > > public EnumDeserializer(Class<E> enumType) { > super(enumType); > this.enumType = enumType; > } > > @Override > public E deserialize(JsonParser jsonParser, DeserializationContext > deserializationContext) throws IOException { > return EnumUtil.of(this.enumType, jsonParser.getIntValue()); > } > > } > > // then , i config MappingJackson2HttpMessageConverter > > SimpleModule customerModule = new SimpleModule(); > > // this setup can be work > > customerModule.addDeserializer(UserStatusEnum.class, new > EnumDeserializer(UserStatusEnum.class)); > // this setup not work > > customerModule.addDeserializer(Enumerable.class, new > EnumDeserializer(Enumerable.class)); > > > who can tell me what's up ? thank you very much. and , this my project in > github : https://github.com/Shiyajian/pretty-boot-demo.git
You can not register super-types for deserializers using `SimpleModule`: type must match exactly. So in this case you would need to register deserializer for `UserStatusEnum.class`, not `Enumerable.class`. This is not a fundamental limitation, however, but just what "Simple" on `SimpleModule` means: due to type-safety limitations it will only do exact matching for deserializers (note that for serializers situation is different: more generic type is fine, so serializers for super types are checked). So if you want to register same deserializer for all Enum types implementing `Enumerable`, you will need to register custom `Deserializers` implementation that defines method "findEnumDeserializer(...)", and register that using Module interface -- you will likely want to implement `Module`, and register custom `Deserializers` in method "setupModule()". Alternatively you could sub-class `SimpleDeserializers`, and use `SimpleModule.setDeserializers(myImpl)`; that should also work. -+ Tatu +- -- You received this message because you are subscribed to the Google Groups "jackson-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. For more options, visit https://groups.google.com/d/optout.
