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

下一篇:静态版通讯录

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...