场景及需求: 项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串。

例如:
[
     {
         "id": 1,
         "name": null
     },
     {
         "id": 2,
         "name": "xiaohong"
     }
]

如上,格式化后的返回内容应该为:
[
     {
         "id": 1,
         "name": ""
     },
     {
         "id": 2,
         "name": "xiaohong"
     }
]

这里直接给出解决方案代码,这里支持FastJson和Jackson配置序列化的方式:

@Configuration
public class WebCatMvcConfiguration extends WebMvcConfigurationSupport { @Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(new ToStringSerializer(Long.TYPE));
module.addSerializer(new ToStringSerializer(Long.class));
module.addSerializer(new ToStringSerializer(BigInteger.class));
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString("");
}
});
objectMapper.registerModule(module);
converter.setObjectMapper(objectMapper); //这里是fastJSON的配置方式,更多的内容可以查看SerializerFeature
// FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
// converter.setFeatures(SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero,
// SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullListAsEmpty);
converters.add(converter);
}
}

最后我们也可以了解一下:WebMvcConfigurationSupport类
下面是它的官方文档描述:

public class WebMvcConfigurationSupport
extends Object
implements ApplicationContextAware, ServletContextAware
This is the main class providing the configuration behind the MVC Java config. It is typically imported by adding @EnableWebMvc to an application@Configuration class. An alternative more advanced option is to extend directly from this class and override methods as necessary remembering to add@Configuration to the subclass and @Bean to overridden @Bean methods. For more details see the Javadoc of @EnableWebMvc.

This class registers the following HandlerMappings:

 

Registers these HandlerAdapters:

Registers a HandlerExceptionResolverComposite with this chain of exception resolvers:

Registers an AntPathMatcher and a UrlPathHelper to be used by:

Note that those beans can be configured with a PathMatchConfigurer.

Both the RequestMappingHandlerAdapter and the ExceptionHandlerExceptionResolver are configured with default instances of the following by default:

【转】SpringBoot自定义序列化的使用方式--WebMvcConfigurationSupport的更多相关文章

  1. SpringBoot自定义序列化的使用方式--WebMvcConfigurationSupport

    场景及需求: 项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串. 例如: [     {         "id": 1,      ...

  2. WeihanLi.Redis自定义序列化及压缩方式

    WeihanLi.Redis自定义序列化及压缩方式 Intro WeihanLi.Redis 是基于 StackExchange.Redis 的扩展,提供了一些常用的业务组件和对泛型的更好支持,默认使 ...

  3. Java 自定义序列化、反序列化

    1.如果某个成员变量是敏感信息,不希望序列化到文件/网络节点中,比如说银行密码,或者该成员变量所属的类是不可序列化的, 可以用 transient 关键字修饰此成员变量,序列化时会忽略此成员变量. c ...

  4. 源码分析springboot自定义jackson序列化,默认null值个性化处理返回值

    最近项目要实现一种需求,对于后端返回给前端的json格式的一种规范,不允许缺少字段和字段值都为null,所以琢磨了一下如何进行将springboot的Jackson序列化自定义一下,先看看如何实现,再 ...

  5. C#序列化与反序列化方式简单总结

    序列化和反序列化 相关类: System.SerializableAttribute特性(或称为属性), System.Runtime.Serialization.ISerializable(自定义序 ...

  6. .Net Core 自定义序列化格式

    序列化对大家来说应该都不陌生,特别是现在大量使用WEBAPI,JSON满天飞,序列化操作应该经常出现在我们的代码上. 而我们最常用的序列化工具应该就是Newtonsoft.Json,当然你用其它工具类 ...

  7. Newtonsoft.Json高级用法 1.忽略某些属性 2.默认值的处理 3.空值的处理 4.支持非公共成员 5.日期处理 6.自定义序列化的字段名称

    手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...

  8. Java Serializable接口(序列化)理解及自定义序列化

      1 Serializable接口 (1)简单地说,就是可以将一个对象(标志对象的类型)及其状态转换为字节码,保存起来(可以保存在数据库,内存,文件等),然后可以在适当的时候再将其状态恢复(也就是反 ...

  9. SpringBoot自定义错误页面,SpringBoot 404、500错误提示页面

    SpringBoot自定义错误页面,SpringBoot 404.500错误提示页面 SpringBoot 4xx.html.5xx.html错误提示页面 ====================== ...

随机推荐

  1. 【WPS单元格】汉字转拼音的方法

    昨晚休息的时候,赵 sir发来消息,突然有急事,需要把大批量的单元格汉字名字转换为拼音.迅粗略搜了下百度,发现office Excel 是很方便的,而赵 sir电脑装的是wps.百度了下,发现关于WP ...

  2. Matlab绘制阶梯形图

    声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 对于Matlab的使用情况常常是这样子的,很多零碎的函数名字很难记忆,经常用过后过一段时间就又忘记了,又得去网 ...

  3. CMake生成VS2010工程相对路径和绝对路径问题说明

    CMake生成VS2010工程相对路径和绝对路径问题说明 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 主要是使用CMake生成的VS2010的工程,最好不 ...

  4. 使用eclipse插件mybatis generator来自动生成实体类及映射文件

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguratio ...

  5. cocos creator 判断滑动方向

    定义变量 public firstX = null; public firsty = null; 点击 获取坐标 this.viewNode.on(cc.Node.EventType.TOUCH_ST ...

  6. docker安装详细步骤-centos7

    https://www.runoob.com/docker/centos-docker-install.html 一.安装docker 1.Docker 要求 CentOS 系统的内核版本高于 3.1 ...

  7. [转帖]超详细的Oracle数据库在不同损坏级别的恢复总结

    超详细的Oracle数据库在不同损坏级别的恢复总结 原创 波波说运维 2019-07-20 00:02:00 概述 在 DBA 的日常工作中不可避免存在着数据库的损坏,今天主要介绍 Oracle 数据 ...

  8. 【C++札记】动态分配内存(malloc,free)

    介绍 操作系统中存在一个内存管理器(Memory Manager),简称MM,它负责管理内存. MM提供的服务:应用程序可以向MM申请一块指定大小的内存(借出),用完之后应用程序应该释放(还回). 所 ...

  9. Erlang:[笔记三,构建工具rebar之使用依赖]

    概述 类似Java中的Maven,Gradle,在Erlang中同样也有包管理的工具,Rebar提供Erlang依赖(包)管理机制,开发人员可以重复使用已有的模块,通过rebar引入自己的项目直接使用 ...

  10. 2019php面试大全

    一 .PHP基础部分 1.PHP语言的一大优势是跨平台,什么是跨平台? PHP的运行环境最优搭配为Apache+MySQL+PHP,此运行环境可以在不同操作系统(例如windows.Linux等)上配 ...