springboot post接口接受json时,转换为对象时,属性都为null的解决
作者:专业矮矬穷 发布时间:2023-06-17 15:24:23
背景
在接口请求过程中,传递json对象,springboot转换为实体VO对象后,所有属性都为null。
post请求:
后台接收请求:
当时就懵逼了…
解决心路历程
查看springboot默认的HttpMessageConverter
@Configuration
@Component
public class AppWebConfiguration implements WebMvcConfigurer {
/**
* 重写添加 * 方法并添加配置 *
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
for (HttpMessageConverter<?> messageConverter : converters) {
System.out.println(messageConverter);
}
}
}
默认的HttpMessageConverter如下:
org.springframework.http.converter.ByteArrayHttpMessageConverter@4ee488a7
org.springframework.http.converter.StringHttpMessageConverter@7c556701
org.springframework.http.converter.StringHttpMessageConverter@1650e1e1
org.springframework.http.converter.ResourceHttpMessageConverter@ffa6a44
org.springframework.http.converter.ResourceRegionHttpMessageConverter@65317aac
org.springframework.http.converter.xml.SourceHttpMessageConverter@328b7464
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@2345d43d
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@7f31325b
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@113bd30b
所以解析jason时,用的转换器应该是MappingJackson2HttpMessageConverter。
进入MappingJackson2HttpMessageConverter进行debug调试,发现,最后转换结果还真是null!
尝试直接用objectMapper转换对象看一下结果
结果惊不惊喜,意不意外~。objectMapper把对象转为json时,属性变为下划线+小写风格了。
难怪对象的属性都为null,压根属性都不是同一个了
看看jackson配置能不能配置转换为驼峰
将命名策略修改为LOWER_CAMEL_CASE。
再看看转换结果:
成功转换为驼峰,对象属性也完美赋值!
将springboot默认的HttpMessageConverter替换为阿里的FastJson转换器MappingJackson2HttpMessageConverter看看效果
@Configuration
public class FastJsonConfiguration {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
List<MediaType> fastMediaTypes = new ArrayList<>();
// 处理中文乱码问题
fastJsonConfig.setCharset(Charset.forName("UTF-8"));
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 设置时间格式
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
// 在转换器中添加配置信息
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter converter = fastJsonHttpMessageConverter;
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setDefaultCharset(Charset.forName("UTF-8"));
stringConverter.setSupportedMediaTypes(fastMediaTypes);
return new HttpMessageConverters(stringConverter, converter);
}
}
再次查看HttpMessageConverter如下:
com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter@15219255
org.springframework.http.converter.ByteArrayHttpMessageConverter@4ee488a7
org.springframework.http.converter.StringHttpMessageConverter@7c556701
org.springframework.http.converter.StringHttpMessageConverter@1650e1e1
org.springframework.http.converter.ResourceHttpMessageConverter@ffa6a44
org.springframework.http.converter.ResourceRegionHttpMessageConverter@65317aac
org.springframework.http.converter.xml.SourceHttpMessageConverter@328b7464
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@2345d43d
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@7f31325b
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@113bd30b
发现FastJsonHttpMessageConverter已经在MappingJackson2HttpMessageConverter之前,springboot序列化会优先采用FastJsonHttpMessageConverter。
再次查看接口解析,发现直接转换到了对象属性中。
来源:https://blog.csdn.net/jiangjun0130/article/details/89210172
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 本文实例讲述了C++语言实现线性表之链表实现方法。分享给大家供大家参考。具体分析如下:插入、删除结点的代码有点多,但这样提高了代码的可读性,
- 目录Navigator 的 push 和 pop方法代码实现最终效果Navigator 的 push 和 pop方法Navigator 导航
- 废话开篇:iOS与android在实现列表界面的时候是有重用机制的,目的就是减少内存开销,用时间换空间。个人感觉flutter并没有特别强调
- [LeetCode] 205. Isomorphic Strings 同构字符串Given two strings s
- 本文主要介绍我为桌面和 Web 设计的一个超级秘密 Flutter 项目使用了画布和可拖动节点界面。本教程将展示我如何使用堆栈来使用小部件完
- 前言通过此篇文章,你将了解到:Flutter windows和Android桌面应用屏幕适配的解决方案;屏幕适配的相关知识和原理;flutt
- 已知字符串“aabbbcddddeeffffghijklmnopqrst”编程找出出现最多的字符和次数,要求时间复杂度小于O(n^2)/**
- 本Demo为练手小项目,主要是熟悉目前主流APP的架构模式.此项目中采用MVC设计模式,纯代码和少许XIB方式实现.主要实现了朋友圈功能和摇
- 前言本文的记录如何用CustomPaint、GestureDetector实现一个进度条控件。首先需要说明的是 flutter Materi
- 图像滤波在opencv中可以有多种实现形式自定义滤波如使用3×3的掩模:对图像进行处理.使用函数filter2D()实现#include&l
- 协议做如下规定:规定数据协议:序列号 长度 状态字 数据长度 数据1 &n
- 前言最近看插件库上少有的取色器大都是圆形的或者奇奇怪的的亚子,所以今天做两个矩形的颜色取色器提示:以下是本篇文章正文内容,下面案例可供参考一
- 首先打开 Visual Studio Installer 可以看到vs2022 只支持安装4.6及以上的版本,如图所示。那么该如何安装4.6
- 1.map遍历快速实现边距,文字自适应改变大小Container( // padding: EdgeI
- 就网络和应用程序而言,键盘快捷键很重要,今天我们要谈的便是让这类快捷键得以在Flutter运作的小部件:Focus、Shortcuts和Ac
- 走马灯是一种常见的效果,本文讲一下如何用 PageView 在 Flutter 里实现一个走马灯, 效果如下,当前页面的高度比其它页面高,切
- forward_list 概述forward_list 是 C++ 11 新增的容器,它的实现为单链表。forward_list 是支持从容
- 本文实例为大家分享了flutter实现appbar下选项卡切换的具体代码,供大家参考,具体内容如下TabBar 、Tab、TabBarVie
- Lambda 表达式Lambda 表达式是现代 C++ 中最重要的特性之一,而 Lambda 表达式,实际上就是提供了一个类似匿名函数的特性
- 已知两个链表list1和list,2,各自非降序排列,将它们合并成另外一个链表list3,并且依然有序,要求保留所有节点。实现过程中,lis