zoukankan      html  css  js  c++  java
  • spring boot 是如何加载jackson的?

    Spring Boot 自动引入jackson: 通过:Spring-Boot-starter-web

      

    Jackson自动配置

     

    这里的configurations是读取的这里:

     

     通过反射加载JacksonAutoConfiguration:

    public void configure(ObjectMapper objectMapper) {
            Assert.notNull(objectMapper, "ObjectMapper must not be null");
    
            MultiValueMap<Object, Module> modulesToRegister = new LinkedMultiValueMap<>();
            if (this.findModulesViaServiceLoader) {
                ObjectMapper.findModules(this.moduleClassLoader).forEach(module -> registerModule(module, modulesToRegister));
            }
            else if (this.findWellKnownModules) {
                registerWellKnownModulesIfAvailable(modulesToRegister);
            }
    
            if (this.modules != null) {
                this.modules.forEach(module -> registerModule(module, modulesToRegister));
            }
            if (this.moduleClasses != null) {
                for (Class<? extends Module> moduleClass : this.moduleClasses) {
                    registerModule(BeanUtils.instantiateClass(moduleClass), modulesToRegister);
                }
            }
            List<Module> modules = new ArrayList<>();
            for (List<Module> nestedModules : modulesToRegister.values()) {
                modules.addAll(nestedModules);
            }
            objectMapper.registerModules(modules);
    
            if (this.dateFormat != null) {
                objectMapper.setDateFormat(this.dateFormat);
            }
            if (this.locale != null) {
                objectMapper.setLocale(this.locale);
            }
            if (this.timeZone != null) {
                objectMapper.setTimeZone(this.timeZone);
            }
    
            if (this.annotationIntrospector != null) {
                objectMapper.setAnnotationIntrospector(this.annotationIntrospector);
            }
            if (this.propertyNamingStrategy != null) {
                objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy);
            }
            if (this.defaultTyping != null) {
                objectMapper.setDefaultTyping(this.defaultTyping);
            }
            if (this.serializationInclusion != null) {
                objectMapper.setSerializationInclusion(this.serializationInclusion);
            }
    
            if (this.filters != null) {
                objectMapper.setFilterProvider(this.filters);
            }
    
            this.mixIns.forEach(objectMapper::addMixIn);
    
            if (!this.serializers.isEmpty() || !this.deserializers.isEmpty()) {
                SimpleModule module = new SimpleModule();
                addSerializers(module);
                addDeserializers(module);
                objectMapper.registerModule(module);
            }
    
            this.visibilities.forEach(objectMapper::setVisibility);
    
            customizeDefaultFeatures(objectMapper);
            this.features.forEach((feature, enabled) -> configureFeature(objectMapper, feature, enabled));
    
            if (this.handlerInstantiator != null) {
                objectMapper.setHandlerInstantiator(this.handlerInstantiator);
            }
            else if (this.applicationContext != null) {
                objectMapper.setHandlerInstantiator(
                        new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory()));
            }
        }

    上面的类中会用到一些yml中的一些配置,是在下面的代码中获取:

     

    在WebMvcConfigurationSupport中可以看到:

    public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {
    
        private static final boolean romePresent;
    
        private static final boolean jaxb2Present;
    
        private static final boolean jackson2Present;
    
        private static final boolean jackson2XmlPresent;
    
        private static final boolean jackson2SmilePresent;
    
        private static final boolean jackson2CborPresent;
    
        private static final boolean gsonPresent;
    
        private static final boolean jsonbPresent;
    
        static {
            ClassLoader classLoader = WebMvcConfigurationSupport.class.getClassLoader();
            romePresent = ClassUtils.isPresent("com.rometools.rome.feed.WireFeed", classLoader);
            jaxb2Present = ClassUtils.isPresent("javax.xml.bind.Binder", classLoader);
            jackson2Present = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) &&
                            ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader);
            jackson2XmlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader);
            jackson2SmilePresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", classLoader);
            jackson2CborPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.cbor.CBORFactory", classLoader);
            gsonPresent = ClassUtils.isPresent("com.google.gson.Gson", classLoader);
            jsonbPresent = ClassUtils.isPresent("javax.json.bind.Jsonb", classLoader);
        }
    }
    protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
            StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
            stringHttpMessageConverter.setWriteAcceptCharset(false);  // see SPR-7316
    
            messageConverters.add(new ByteArrayHttpMessageConverter());
            messageConverters.add(stringHttpMessageConverter);
            messageConverters.add(new ResourceHttpMessageConverter());
            messageConverters.add(new ResourceRegionHttpMessageConverter());
            try {
                messageConverters.add(new SourceHttpMessageConverter<>());
            }
            catch (Throwable ex) {
                // Ignore when no TransformerFactory implementation is available...
            }
            messageConverters.add(new AllEncompassingFormHttpMessageConverter());
    
            if (romePresent) {
                messageConverters.add(new AtomFeedHttpMessageConverter());
                messageConverters.add(new RssChannelHttpMessageConverter());
            }
    
            if (jackson2XmlPresent) {
                Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
                if (this.applicationContext != null) {
                    builder.applicationContext(this.applicationContext);
                }
                messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
            }
            else if (jaxb2Present) {
                messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
            }
    
            if (jackson2Present) {
                Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
                if (this.applicationContext != null) {
                    builder.applicationContext(this.applicationContext);
                }
                messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));
            }
            else if (gsonPresent) {
                messageConverters.add(new GsonHttpMessageConverter());
            }
            else if (jsonbPresent) {
                messageConverters.add(new JsonbHttpMessageConverter());
            }
    
            if (jackson2SmilePresent) {
                Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.smile();
                if (this.applicationContext != null) {
                    builder.applicationContext(this.applicationContext);
                }
                messageConverters.add(new MappingJackson2SmileHttpMessageConverter(builder.build()));
            }
            if (jackson2CborPresent) {
                Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.cbor();
                if (this.applicationContext != null) {
                    builder.applicationContext(this.applicationContext);
                }
                messageConverters.add(new MappingJackson2CborHttpMessageConverter(builder.build()));
            }
        }

    在使用Jackson进行序列化和反序列化时主要是使用MappingJackson2SmileHttpMessageConverter进行的,下一篇详细介绍一下。

  • 相关阅读:
    简单的登录验证小程序_python
    远程执行命令_python
    远程执行本地脚本_linux
    反射_python
    ssh oa项目介绍
    返回上一级过程
    ssh框架开发crm(客户关系系统总结)
    struct相对路径,绝对路径
    <s:textfield>标签回显
    ssh框架整合
  • 原文地址:https://www.cnblogs.com/hankuikui/p/11592471.html
Copyright © 2011-2022 走看看