springboot post接口接受json时,转换为对象时,属性都为null的解决

作者:专业矮矬穷 时间:2023-06-17 15:24:23 

背景

在接口请求过程中,传递json对象,springboot转换为实体VO对象后,所有属性都为null。

post请求:

springboot post接口接受json时,转换为对象时,属性都为null的解决

后台接收请求:

springboot post接口接受json时,转换为对象时,属性都为null的解决

当时就懵逼了…

解决心路历程

查看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!

springboot post接口接受json时,转换为对象时,属性都为null的解决

尝试直接用objectMapper转换对象看一下结果

springboot post接口接受json时,转换为对象时,属性都为null的解决

结果惊不惊喜,意不意外~。objectMapper把对象转为json时,属性变为下划线+小写风格了。

难怪对象的属性都为null,压根属性都不是同一个了

看看jackson配置能不能配置转换为驼峰

springboot post接口接受json时,转换为对象时,属性都为null的解决

将命名策略修改为LOWER_CAMEL_CASE。

springboot post接口接受json时,转换为对象时,属性都为null的解决

再看看转换结果:

springboot post接口接受json时,转换为对象时,属性都为null的解决

成功转换为驼峰,对象属性也完美赋值!

将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

标签:springboot,json,post,null
0
投稿

猜你喜欢

  • Flutter 剪裁组件的使用

    2023-06-18 13:15:04
  • 使用Flutter实现一个走马灯布局的示例代码

    2023-06-19 03:50:03
  • C++实现LeetCode(205.同构字符串)

    2023-06-21 04:06:54
  • Flutter模仿实现微信底部导航栏流程详解

    2023-06-21 11:46:12
  • 详解Android Flutter中SliverAppBar的使用教程

    2023-06-23 12:11:27
  • 在Flutter中制作翻转卡片动画的完整实例代码

    2023-06-23 23:31:21
  • Flutter app页面路由以及路由拦截的实现

    2023-06-23 14:21:18
  • Swift洗牌动画效果的实现方法

    2023-06-21 14:01:56
  • Flutter开发中的路由参数处理

    2023-06-21 04:27:48
  • OpenCV画任意圆弧曲线

    2023-06-22 19:28:44
  • 详解C++ STL模拟实现forward_list

    2023-06-21 02:36:04
  • flutter实现appbar下选项卡切换

    2023-06-21 13:35:24
  • OpenCV实现直线拟合

    2023-06-22 15:22:37
  • C语言字符串另类用法的实现

    2023-06-19 02:05:25
  • Flutter网络请求的3种简单实现方法

    2023-06-21 10:53:22
  • 替换so文件来动态替换Flutter代码实现详解

    2023-06-23 16:24:06
  • 使用flutter创建可移动的stack小部件功能

    2023-06-21 12:28:25
  • Flutter实现抽屉动画

    2023-06-18 01:49:19
  • Flutter应用集成极光推送的实现示例

    2023-06-24 03:51:04
  • Linux下g++编译与使用静态库和动态库的方法

    2023-06-21 13:41:46
  • asp之家 软件编程 m.aspxhome.com