mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-06-06 14:03:40 +08:00
feat: 优化对象序列化
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
package org.hswebframework.web.starter.jackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JacksonException;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.deser.BeanDeserializerBuilder;
|
||||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
|
||||
import com.fasterxml.jackson.databind.module.SimpleDeserializers;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.databind.type.ClassKey;
|
||||
import com.fasterxml.jackson.databind.type.ReferenceType;
|
||||
import org.hswebframework.web.api.crud.entity.EntityFactory;
|
||||
import org.hswebframework.web.dict.EnumDict;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
@@ -15,6 +21,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.codec.CodecConfigurer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@AutoConfigureAfter(JacksonAutoConfiguration.class)
|
||||
public class CustomCodecsAutoConfiguration {
|
||||
@@ -31,6 +39,32 @@ public class CustomCodecsAutoConfiguration {
|
||||
// objectMapper.setTypeFactory(new CustomTypeFactory(entityFactory));
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.setDeserializers(new SimpleDeserializers() {
|
||||
|
||||
@Override
|
||||
public JsonDeserializer<?> findBeanDeserializer(JavaType type,
|
||||
DeserializationConfig config,
|
||||
BeanDescription beanDesc) throws JsonMappingException {
|
||||
JsonDeserializer<?> deserializer = super.findBeanDeserializer(type, config, beanDesc);
|
||||
|
||||
if (deserializer == null) {
|
||||
|
||||
Class clazz = entityFactory.getInstanceType(type.getRawClass(), false);
|
||||
|
||||
if (clazz == null || clazz == type.getRawClass()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
addDeserializer((Class) type.getRawClass(), new JsonDeserializer<Object>() {
|
||||
@Override
|
||||
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
|
||||
return p.readValueAs(clazz);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return super.findBeanDeserializer(type, config, beanDesc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonDeserializer<?> findEnumDeserializer(Class<?> type,
|
||||
DeserializationConfig config,
|
||||
@@ -54,7 +88,7 @@ public class CustomCodecsAutoConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
CustomMappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(EntityFactory entityFactory,ObjectMapper objectMapper) {
|
||||
CustomMappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(EntityFactory entityFactory, ObjectMapper objectMapper) {
|
||||
return new CustomMappingJackson2HttpMessageConverter(objectMapper, entityFactory);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user