SpringMVC的Jackson全局空值处理

java 复制代码
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 {
    @Override
    protected 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的组件,如视图解析器、异常处理器等.
相关推荐
hello早上好20 分钟前
JDK 代理原理
java·spring boot·spring
何苏三月1 小时前
SpringCloud系列 - Sentinel 服务保护(四)
spring·spring cloud·sentinel
纳兰青华1 小时前
bean注入的过程中,Property of ‘java.util.ArrayList‘ type cannot be injected by ‘List‘
java·开发语言·spring·list
魔芋红茶2 小时前
spring-initializer
python·学习·spring
zzywxc7873 小时前
AI 正在深度重构软件开发的底层逻辑和全生命周期,从技术演进、流程重构和未来趋势三个维度进行系统性分析
java·大数据·开发语言·人工智能·spring
llwszx8 小时前
深入理解Java锁原理(一):偏向锁的设计原理与性能优化
java·spring··偏向锁
麦兜*12 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
CHENWENFEIc15 小时前
SpringBoot论坛系统安全测试实战报告
spring boot·后端·程序人生·spring·系统安全·安全测试
高兴达15 小时前
RPC--Netty客户端实现
java·spring·rpc
要开心吖ZSH19 小时前
《Spring 中上下文传递的那些事儿》Part 4:分布式链路追踪 —— Sleuth + Zipkin 实践
java·分布式·spring