当前位置: 首页> 教育> 幼教 > 天津广告设计公司排名_手工制作房子硬纸板_seo网站页面优化包含_免费推广网站平台

天津广告设计公司排名_手工制作房子硬纸板_seo网站页面优化包含_免费推广网站平台

时间:2025/7/12 15:44:30来源:https://blog.csdn.net/2301_78488802/article/details/144935617 浏览次数:0次
天津广告设计公司排名_手工制作房子硬纸板_seo网站页面优化包含_免费推广网站平台
package com.gy.subject.application.config;import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import java.util.List;/*** mvc配置* @author 高悦* @version 1.0* @description: TODO* @date 2025/1/4 17:50*/
@Configuration
public class GlobalConfig extends WebMvcConfigurationSupport {@Overrideprotected void configureMessageConverters(List<HttpMessageConverter<?>> converters){super.configureMessageConverters(converters);converters.add(mappingJackson2HttpMessageConverter());}private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {ObjectMapper objectMapper = new ObjectMapper();objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);return new MappingJackson2HttpMessageConverter(objectMapper);}}

类定义

  • GlobalConfig
    • 包名com.gy.subject.application.config,表示这个配置类属于项目的配置包.
    • 注解@Configuration,表示这是一个Spring的配置类,Spring容器会在启动时加载并应用这个类中定义的配置.
    • 继承WebMvcConfigurationSupport,这是一个Spring MVC的配置支持类,提供了许多用于自定义Spring MVC行为的方法,如配置消息转换器、视图解析器等.

方法

  • configureMessageConverters

    • 参数List<HttpMessageConverter<?>> converters,这是一个列表,用于存储Spring MVC的消息转换器.
    • 功能
      • 调用super.configureMessageConverters(converters)来应用Spring MVC的默认消息转换器配置.
      • 调用converters.add(mappingJackson2HttpMessageConverter())将自定义的MappingJackson2HttpMessageConverter添加到消息转换器列表中,以便在Spring MVC中使用.
  • mappingJackson2HttpMessageConverter

    • 功能:创建并配置一个MappingJackson2HttpMessageConverter实例.
    • 步骤
      • 创建一个ObjectMapper实例,这是Jackson库的核心类,用于处理JSON的序列化和反序列化.
      • 配置ObjectMapper
        • objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false):关闭FAIL_ON_EMPTY_BEANS特性,表示在序列化空对象时不抛出异常,允许序列化空对象.
        • objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL):设置序列化包含策略为NON_NULL,表示在序列化对象时,只包含非空属性,忽略空属性.
      • 返回一个使用配置好的ObjectMapper实例的MappingJackson2HttpMessageConverter对象.

作用

  • 自定义JSON序列化行为:通过配置ObjectMapper,你可以自定义Spring MVC中JSON的序列化行为,例如忽略空属性、处理空对象等.
  • 扩展Spring MVC配置:通过继承WebMvcConfigurationSupport并重写configureMessageConverters方法,你可以扩展Spring MVC的默认配置,添加自定义的消息转换器,从而更好地满足项目的需求.

注意事项

  • 版本兼容性:确保使用的Spring版本与代码兼容,特别是WebMvcConfigurationSupport类的使用.
  • 配置优先级:如果你使用了Spring Boot的自动配置,可能需要考虑配置的优先级,确保自定义配置能够正确应用.
  • 其他配置:除了消息转换器,还可以根据需要配置其他Spring MVC的组件,如视图解析器、异常处理器等.
关键字:天津广告设计公司排名_手工制作房子硬纸板_seo网站页面优化包含_免费推广网站平台

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: