springboot控制层消息null值处理
创始人
2025-05-31 19:28:55
0

大概了解三种方式:

1.区别在于WebMvcConfigurerAdapter 新版本过期,不建议使用。

2.剩下两种不建议同时使用,WebMvcConfigurationSupport是webmvc的配置类,如果在springboot项目中,有类继承了WebMvcConfigurationSupport,那么webmvc的自动配置类WebMvcAutoConfiguration就会失效,导致挂载页面访问不到。

3.如果继承WebMvcConfigurer重写configureMessageConverters但没生效,转换器是列表添加的形式,add进去后就是最后一个,所以没有生效。添加到第一位即可

converters.add(converter);改为converters.add(0,converter);

1.继承WebMvcConfigurerAdapter


import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ming.core.utils.SpringBeanManagerUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;/*** 拦截器配置*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {@Overridepublic void extendMessageConverters(List> converters) {FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();converters.add(fastJsonHttpMessageConverter);        }
}

2.实现WebMvcConfigurer接口

@EnableWebMvc
@RequiredArgsConstructor
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {private final RequestIntercept requestIntercept;@Overridepublic void configureMessageConverters(List> converters) {// 1.需要定义一个convert转换消息的对象;FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();// 2:添加fastJson的配置信息;FastJsonConfig fastJsonConfig = new FastJsonConfig();// 把值是空的直接过滤掉,去掉循环转换fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullBooleanAsFalse);// 4.在convert中添加配置信息.fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);HttpMessageConverter converter = fastJsonHttpMessageConverter;converters.add(converter);}}

3.继承WebMvcConfigurationSupport

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;/*** @CreateTime: 2023-03-20 16:10* @Description: TODO*/
@Configuration
public class JsonMessageConfig extends WebMvcConfigurationSupport {/*** 使用阿里 fastjson 作为JSON MessageConverter* @param converters*/@Overridepublic void configureMessageConverters(List> converters) {FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();FastJsonConfig config = new FastJsonConfig();config.setSerializerFeatures(// 保留map空的字段SerializerFeature.WriteMapNullValue,// 将String类型的null转成""SerializerFeature.WriteNullStringAsEmpty,// 将Number类型的null转成0SerializerFeature.WriteNullNumberAsZero,// 将List类型的null转成[]SerializerFeature.WriteNullListAsEmpty,// 将Boolean类型的null转成falseSerializerFeature.WriteNullBooleanAsFalse,// 避免循环引用SerializerFeature.DisableCircularReferenceDetect);converter.setFastJsonConfig(config);converter.setDefaultCharset(StandardCharsets.UTF_8);List mediaTypeList = new ArrayList<>();// 解决中文乱码问题,相当于在Controller上的@RequestMapping中加了个属性produces = "application/json"mediaTypeList.add(MediaType.APPLICATION_JSON);converter.setSupportedMediaTypes(mediaTypeList);converters.add(converter);}
}

WebMvcConfigurationSupportWebMvcConfigurer都是Spring MVC框架中用于配置WebMvc的接口/类,但是它们有一些不同之处:

  1. WebMvcConfigurationSupport是一个类,而WebMvcConfigurer是一个接口。WebMvcConfigurationSupport提供了一个基本的WebMvc配置类,您可以继承该类并覆盖它的方法来自定义您的配置。WebMvcConfigurer则是一个接口,它定义了一系列回调方法,您可以在您的应用程序中实现这些方法以自定义Spring MVC的配置。
  2. WebMvcConfigurationSupport可以用来配置全局的WebMvc配置,例如ViewResolverArgumentResolver等。您可以使用@EnableWebMvc注解来启用它。而WebMvcConfigurer则可以用于配置局部的WebMvc配置,例如添加拦截器、解析器等。您可以使用@Configuration注解配合@EnableWebMvc注解来启用它。
  3. WebMvcConfigurationSupport可以使用@Autowired注解来注入其他的Bean,例如ConversionServiceValidator等。而WebMvcConfigurer则不能使用@Autowired注解,因为它是一个接口。 总的来说,WebMvcConfigurationSupport提供了更全面的WebMvc配置,但是它也更为复杂,适用于全局的配置。而WebMvcConfigurer则提供了更为灵活的配置方式,适用于局部的配置。

相关内容

热门资讯

【MySQL】锁 锁 文章目录锁全局锁表级锁表锁元数据锁(MDL)意向锁AUTO-INC锁...
【内网安全】 隧道搭建穿透上线... 文章目录内网穿透-Ngrok-入门-上线1、服务端配置:2、客户端连接服务端ÿ...
GCN的几种模型复现笔记 引言 本篇笔记紧接上文,主要是上一篇看写了快2w字,再去接入代码感觉有点...
数据分页展示逻辑 import java.util.Arrays;import java.util.List;impo...
Redis为什么选择单线程?R... 目录专栏导读一、Redis版本迭代二、Redis4.0之前为什么一直采用单线程?三、R...
【已解决】ERROR: Cou... 正确指令: pip install pyyaml
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
Lock 接口解读 前置知识点Synchronized synchronized 是 Java 中的关键字,...
Win7 专业版安装中文包、汉... 参考资料:http://www.metsky.com/archives/350.htm...
3 ROS1通讯编程提高(1) 3 ROS1通讯编程提高3.1 使用VS Code编译ROS13.1.1 VS Code的安装和配置...
大模型未来趋势 大模型是人工智能领域的重要发展趋势之一,未来有着广阔的应用前景和发展空间。以下是大模型未来的趋势和展...
python实战应用讲解-【n... 目录 如何在Python中计算残余的平方和 方法1:使用其Base公式 方法2:使用statsmod...
学习u-boot 需要了解的m... 一、常用函数 1. origin 函数 origin 函数的返回值就是变量来源。使用格式如下...
常用python爬虫库介绍与简... 通用 urllib -网络库(stdlib)。 requests -网络库。 grab – 网络库&...
药品批准文号查询|药融云-中国... 药品批文是国家食品药品监督管理局(NMPA)对药品的审评和批准的证明文件...
【2023-03-22】SRS... 【2023-03-22】SRS推流搭配FFmpeg实现目标检测 说明: 外侧测试使用SRS播放器测...
有限元三角形单元的等效节点力 文章目录前言一、重新复习一下有限元三角形单元的理论1、三角形单元的形函数(Nÿ...
初级算法-哈希表 主要记录算法和数据结构学习笔记,新的一年更上一层楼! 初级算法-哈希表...
进程间通信【Linux】 1. 进程间通信 1.1 什么是进程间通信 在 Linux 系统中,进程间通信...
【Docker】P3 Dock... Docker数据卷、宿主机与挂载数据卷的概念及作用挂载宿主机配置数据卷挂载操作示例一个容器挂载多个目...