Jackson 多态序列化可以通过@JsonSubtypes来实现,但总觉得不是很方便,比如新增子类的时候都要去加一下JsonSubTypes。
网上找了下,发现通过实现TypeIdResolver 可以自定义序列化的一些操作,上代码
public class JacksonTypeIdResolver implements TypeIdResolver {
private JavaType baseType;
@Override
public void init(JavaType javaType) {
baseType = javaType;
}
@Override
public String idFromValue(Object o) {
return idFromValueAndType(o, o.getClass());
}
@Override
public String idFromValueAndType(Object o, Class<?> aClass) {
//有出现同名类时可以用这个来做区别
JsonTypeName annotation = aClass.getAnnotation(JsonTypeName.class);
if (annotation != null) {
return annotation.value();
}
String name = aClass.getName();
String[] splits = StringUtils.split(name, ".");
String className = splits[splits.length - 1];
return className;
}
@Override
public JavaType typeFromId(DatabindContext databindContext, String type) {
Class<?> clazz = getSubType(type);
if (clazz == null) {
throw new IllegalStateException("cannot find class '" + type + "'");
}
return databindContext.constructSpecializedType(baseType, clazz);
}
public Class<?> getSubType(String type) {
Reflections reflections = ReflectionsCache.getReflections();
Set<Class<?>> subTypes = reflections.getSubTypesOf((Class<Object>) baseType.getRawClass());
for (Class<?> subType : subTypes) {
JsonTypeName annotation = subType.getAnnotation(JsonTypeName.class);
if (annotation != null && annotation.value().equals(type)) {
return subType;
} else if (subType.getSimpleName().equals(type)) {
return subType;
}
else if(subType.getName().equals(type)){
return subType;
}
}
return null;
}
@Override
public String idFromBaseType() {
return idFromValueAndType(null, baseType.getClass());
}
@Override
public String getDescForKnownTypeIds() {
return null;
}
@Override
public JsonTypeInfo.Id getMechanism() {
return JsonTypeInfo.Id.CUSTOM;
}
}
这里面主要的2个方法 idFromValueAndType和typeFromId
idFromValueAndType 是序列化的时候告诉序列化器怎么生成标识符
typeFromId是反序列化的时候告诉序列化器怎么根据标识符来识别到具体类型,这里用了反射,在程序启动时,把要加载的包通过Reflections加载进来
@Service
public class ClassCacheInitializing implements InitializingBean {
private String packages="com.doudanhua.basecode";
/**
* 反射会有点耗时,所以程序启动的时候加载完放到缓存里面去,后面要用的时候直接去缓存取
* @throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
Reflections reflections = new Reflections(packages);
if (reflections != null) {
ReflectionsCache.setReflections(reflections);
}
}
}
public class ReflectionsCache {
private static Reflections reflections;
public static void setReflections(Reflections _reflections) {
reflections = _reflections;
}
public static Reflections getReflections() {
return reflections;
}
}
测试:
@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, property = "@class")
@JsonTypeIdResolver(JacksonTypeIdResolver.class)
public class Animal {
private String name;
}
@Data
public class Dog extends Animal {
private int age;
}
@Data
public class AnimalDto {
private Animal animal;
}
@Test
public void json_test() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
AnimalDto animalDto = new AnimalDto();
Dog dog = new Dog();
dog.setName("dog");
dog.setAge(1);
animalDto.setAnimal(dog);
String s = objectMapper.writeValueAsString(animalDto);
System.out.print(s);
animalDto = objectMapper.readValue(s, AnimalDto.class);
assert animalDto != null;
}
测试结果:
{"animal":{"@class":"Dog","name":"dog","age":1}} 反序列化也正常
最后说明下,如果有出现同名类,可以用@JsonTypeName来区别