【转】SpringBoot自定义序列化的使用方式--WebMvcConfigurationSupport
场景及需求: 项目接入了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
@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 HandlerMapping
s:
RequestMappingHandlerMapping
ordered at 0 for mapping requests to annotated controller methods.HandlerMapping
ordered at 1 to map URL paths directly to view names.BeanNameUrlHandlerMapping
ordered at 2 to map URL paths to controller bean names.HandlerMapping
ordered atInteger.MAX_VALUE-1
to serve static resource requests.HandlerMapping
ordered atInteger.MAX_VALUE
to forward requests to the default servlet.
Registers these HandlerAdapter
s:
RequestMappingHandlerAdapter
for processing requests with annotated controller methods.HttpRequestHandlerAdapter
for processing requests withHttpRequestHandler
s.SimpleControllerHandlerAdapter
for processing requests with interface-basedController
s.
Registers a HandlerExceptionResolverComposite
with this chain of exception resolvers:
ExceptionHandlerExceptionResolver
for handling exceptions through @ExceptionHandler
methods.ResponseStatusExceptionResolver
for exceptions annotated with @ResponseStatus
.DefaultHandlerExceptionResolver
for resolving known Spring exception types
Registers an AntPathMatcher
and a UrlPathHelper
to be used by:
- the
RequestMappingHandlerMapping
, - the
HandlerMapping
for ViewControllers - and the
HandlerMapping
for serving resources
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:
- a
ContentNegotiationManager
- a
DefaultFormattingConversionService
- a
OptionalValidatorFactoryBean
if a JSR-303 implementation is available on the classpath - a range of
HttpMessageConverter
s depending on the third-party libraries available on the classpath.
【转】SpringBoot自定义序列化的使用方式--WebMvcConfigurationSupport的更多相关文章
- SpringBoot自定义序列化的使用方式--WebMvcConfigurationSupport
场景及需求: 项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串. 例如: [ { "id": 1, ...
- WeihanLi.Redis自定义序列化及压缩方式
WeihanLi.Redis自定义序列化及压缩方式 Intro WeihanLi.Redis 是基于 StackExchange.Redis 的扩展,提供了一些常用的业务组件和对泛型的更好支持,默认使 ...
- Java 自定义序列化、反序列化
1.如果某个成员变量是敏感信息,不希望序列化到文件/网络节点中,比如说银行密码,或者该成员变量所属的类是不可序列化的, 可以用 transient 关键字修饰此成员变量,序列化时会忽略此成员变量. c ...
- 源码分析springboot自定义jackson序列化,默认null值个性化处理返回值
最近项目要实现一种需求,对于后端返回给前端的json格式的一种规范,不允许缺少字段和字段值都为null,所以琢磨了一下如何进行将springboot的Jackson序列化自定义一下,先看看如何实现,再 ...
- C#序列化与反序列化方式简单总结
序列化和反序列化 相关类: System.SerializableAttribute特性(或称为属性), System.Runtime.Serialization.ISerializable(自定义序 ...
- .Net Core 自定义序列化格式
序列化对大家来说应该都不陌生,特别是现在大量使用WEBAPI,JSON满天飞,序列化操作应该经常出现在我们的代码上. 而我们最常用的序列化工具应该就是Newtonsoft.Json,当然你用其它工具类 ...
- Newtonsoft.Json高级用法 1.忽略某些属性 2.默认值的处理 3.空值的处理 4.支持非公共成员 5.日期处理 6.自定义序列化的字段名称
手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...
- Java Serializable接口(序列化)理解及自定义序列化
1 Serializable接口 (1)简单地说,就是可以将一个对象(标志对象的类型)及其状态转换为字节码,保存起来(可以保存在数据库,内存,文件等),然后可以在适当的时候再将其状态恢复(也就是反 ...
- SpringBoot自定义错误页面,SpringBoot 404、500错误提示页面
SpringBoot自定义错误页面,SpringBoot 404.500错误提示页面 SpringBoot 4xx.html.5xx.html错误提示页面 ====================== ...
随机推荐
- Leetcode之148. Sort List Medium
https://leetcode.com/problems/sort-list/ Sort a linked list in O(n log n) time using constant space ...
- C/C++.控制台输入(cin/getchar)
1.类似的函数有:cin.getchar.fgetc 等 2.问题: 最后的"\n"都不取出来... 2.1.对策:(ZC:下面是 我自己使用后的感受) (1)fflush(std ...
- Android核心程序之SystemUI - (一)开篇
UI是实现用户交互的重要途径之一,而Android中一个重要的UI元素就是SystemUI,本文分析基于Android 5.1,分析SystemUI的启动及运行过程. SystemUI源代码所在路径为 ...
- 最新 房天下java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.房天下等10家互联网公司的校招Offer,因为某些自身原因最终选择了房天下.6.7月主要是做系统复习.项目复盘.LeetCo ...
- Memcached stats命令及核心参数
一.stats命令 用来查看服务器的运行状态和内部数据,其中核心的参数有: 1.缓存命中率相关参数: cmd_get:总查询次数 get_hits:命中次数 get_misses:未命中次数 2.使用 ...
- js中实现base64加密、解密
//base64加密 解密 /* //1.加密 var result = Base.encode('125中文'); //--> "MTI15Lit5paH" //2.解密 ...
- HTTP网页请求状态码
我们平时在打开一些网页的时候,会遇到打不开的情况,页面提示404错误,这个404就是http状态码.如果我们可以正常打开网页,这时也会有http状态码的,这个状态码就是200,只不过这时我们是无法通过 ...
- 【单元测试框架unittest】
- Feeding Time 【bfs求最大连通块】
题目链接:https://ac.nowcoder.com/acm/contest/1870/J 题目大意:求最大的连通块是多大 主要是为了防止自己忘记bfs怎么写..... #include<s ...
- WUSTOJ 1239: n皇后问题(Java)
题目链接: