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