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


猜你喜欢
- 修订功能可以跟踪文档所有的修改,了解修改的过程,这对于团队协同文档编辑、审阅是非常有用的一个功能。将工作簿发送给他人审阅时,我们可以开启修订
- 在J2EE项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免会遇到各种可预知的、不可预知的异
- mybatis映射和实际类型不一致项目今天出现个问题,在dao中定义了一个查询,方法的返回值是map并定义了泛型都是String类型,可是方
- 一、文件的编码package com.study.io;/*** 测试文件编码*/public class EncodeDemo {/***
- eclipse中改变默然的workspace的方法可以有以下几种:1.在创建project的时候,手动选择使用新的workspace,如创建
- 在方法声明中只允许一个paras关键字,并且该关键字只能为最后一个。using System; /*********************
- 一般来说,在更新DataTable或是DataSet时,如果不采用SqlParameter,那么当输入的Sql语句出现歧义时,如字符串中含有
- 前言数据库的性能优化行业里面普遍偏少,今天这篇希望给大家带来点帮助SQLite是个典型的嵌入式DBMS,它有很多优点,它是轻量级的,在编译之
- using System;using System.Collections.Generic;using System.Globalizati
- 题目给定count=0;让5个线程并发累加到1000;思路创建一个类MyRunnable,实现Runnable(继承Thread类也可)定义
- 一、前言Android 实现卫星式菜单也叫弧形菜单,主要要做的工作如下:1.动画的处理2.自定义ViewGroup来实现卫星式菜单View(
- 本文演示android中图片加载到内存首先设计界面:代码如下:<LinearLayout xmlns:android="ht
- 新建多国语言包要在android studio项目中新建多国语言包,有两种方式,一种是手动建,一种是用使用android studio辅助建
- 最近学习了继承,多态,集合,设计模式,有一个汽车租凭系统,给大家分享一下:我们首先来看看我们这个系统的效果1.做一个项目,我们首先对项目进行
- 对于从事Android开发的人来说,遇到ANR(Application Not Responding)是比较常见的问题。一般情况下,如果有A
- Android通过访问网页查看网页源码1.添加网络权限<!--访问网络的权限--> <uses-permission an
- 今天朋友圈又火了,听说原因是 @腾讯官网 就能得到一顶绿色的帽子,啊呸,是一个好看的国庆节头像,可是听说没一会就502了,那么我们自己动手实
- 1.注解方式,yml文件配置上以下就可以直接使用mybatis-plus: mapper-locations: classpath:mapp
- 当我保持对连续将对象拖有时在移动后 5 6 拖/滴,看到有时不获取对象还原不回来,我不能用于以后。基本上我有对两个对象组的 canvas 在
- 前言前天工作中遇到了这样一个问题,我在接口的参数封装了一个pojo,这是很常见的,当参数一多,惯性的思维就是封装一个pojo.那么在参数前有