springboot控制层消息null值处理
创始人
2025-05-31 20:32:53
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则提供了更为灵活的配置方式,适用于局部的配置。

上一篇:常用hook

下一篇:静态版通讯录

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...