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

 更新时间:2021年10月19日 11:06:40   作者:专业矮矬穷  
这篇文章主要介绍了springboot post接口接受json时,转换为对象时,属性都为null的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

背景

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

post请求:

postman调用

后台接收请求:

后台接收请求

当时就懵逼了…

解决心路历程

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

再次查看接口解析,发现直接转换到了对象属性中。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java实现递归读取文件夹下的所有文件

    Java实现递归读取文件夹下的所有文件

    这篇文章主要为大家详细介绍了如何利用Java实现递归读取文件夹下的所有文件,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-02-02
  • 修改maven项目端口号的方法

    修改maven项目端口号的方法

    今天小编就为大家分享一篇修改maven项目端口号的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • 一文详解Spring拦截链的实现原理

    一文详解Spring拦截链的实现原理

    在 Web应用开发中,拦截器(Interceptor)是一种非常重要的机制,能够在请求处理的各个阶段进行前置和后置处理,本文主要来探讨一下 Spring 拦截链的实现原理,需要的可以了解下
    2025-01-01
  • Spring中的singleton和prototype的实现

    Spring中的singleton和prototype的实现

    这篇文章主要介绍了Spring中的singleton和prototype的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • 多模块maven的deploy集成gitlab ci自动发版配置

    多模块maven的deploy集成gitlab ci自动发版配置

    这篇文章主要为大家介绍了多模块maven项目deploy集成gitlab ci自动发版的配置流程步骤,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2022-02-02
  • 详解Spring Cloud 熔断机制--断路器

    详解Spring Cloud 熔断机制--断路器

    这篇文章主要介绍了详解Spring Cloud 熔断机制--断路器,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • Springboot+Redis执行lua脚本的项目实践

    Springboot+Redis执行lua脚本的项目实践

    本文主要介绍了Springboot+Redis执行lua脚本的项目实践,详细的介绍Redis与Lua脚本的结合应用,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • 如何在Java中实现一个散列表

    如何在Java中实现一个散列表

    这篇文章主要介绍了如何在Java中实现一个散列表,建一个HashMap,以String类型为Key,Int类型为Value,下文具体的操作过程需要的小伙伴可以参考一下
    2022-04-04
  • 深入理解Java设计模式之简单工厂模式

    深入理解Java设计模式之简单工厂模式

    这篇文章主要介绍了JAVA设计模式之简单工厂模式的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下
    2021-11-11
  • 解决java文件流处理异常 mark/reset not supported问题

    解决java文件流处理异常 mark/reset not supported问题

    这篇文章主要介绍了解决java文件流处理异常 mark/reset not supported问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10

最新评论