使用FastJsonHttpMessageConverter解析@RequestBody参数
创始人
2025-05-29 11:02:36
0

文章目录

  • 1、使用方式
  • 2、FastJsonHttpMessageConverter 不生效问题
    • 解决措施
  • 3、作用
    • 1)解析文本格式的json字符串参数
    • 2)参数下划线转驼峰
  • 4、实现方式
    • 1)FastJsonHttpMessageConverter#canRead()
    • 2)FastJsonHttpMessageConverter#read()

1、使用方式

1> pom中引入fastjson相关依赖:

com.alibabafastjson1.2.77

2> 添加FastJsonHttpMessageConverter类型转换器配置(这种配置方式是无效的):

@Configuration
public class FastJsonMessageConverterConfig implements WebMvcConfigurer{public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {return new FastJsonHttpMessageConverter();}@Overridepublic void configureMessageConverters(List> converters) {converters.add(fastJsonHttpMessageConverter());}
}

3> JSON字符串对应的JAVA模型:

@Data
public class Person {private String name;private Integer age;private Boolean sex;private String otherInfo;
}

4> 后端Controller接口:

在这里插入图片描述

2、FastJsonHttpMessageConverter 不生效问题

@Configuration
public class FastJsonMessageConverterConfig implements WebMvcConfigurer{public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {return new FastJsonHttpMessageConverter();}@Overridepublic void configureMessageConverters(List> converters) {converters.add(fastJsonHttpMessageConverter());}
}

使用上述配置方式配置FastJsonHttpMessageConverter,其不会生效,解析JSON类型参数依旧是使用的MappingJackson2HttpMessageConverter。

因为自定义的FastJsonHttpMessageConverter 排在所有MessageConverter的最后;

  • PS:《传送链接》Spring MVC 如何解析各种参数数据(3月13日早8点发出)
    在这里插入图片描述
  • 根据消息转换器的应用规则,会顺序选择符合要求的消息转换器,当有一个合适的MessageConverter之后,不会再看后面的MessageConverter。
  • 而MappingJackson2HttpMessageConverter 在 FastJsonHttpMessageConverter 之前,所以会使用MappingJackson2HttpMessageConverter 进行消息转换 ;

解决措施

解决措施很简单:将FastJsonHttpMessageConverter消息转换器 添加在MappingJackson2HttpMessageConverter 前面;

@Configuration
public class FastJsonMessageConverterConfig{@Beanpublic HttpMessageConverters fastJsonHttpMessageConverters() {return new HttpMessageConverters(new FastJsonHttpMessageConverter());}
}

效果:

在这里插入图片描述

3、作用

用于解析JSON类型参数的FastJsonHttpMessageConverter相比于Spring MVC原生携带的MappingJackson2HttpMessageConverter多了两个作用:

  1. FastJson可以解析文本格式的JSON字符串参数,自动给json请求添加请求头:Content-Type:application/json;
  2. 将参数下划线转为驼峰; 比如:other_info 转为 otherInfo

1)解析文本格式的json字符串参数

在这里插入图片描述

2)参数下划线转驼峰

在这里插入图片描述

4、实现方式

FastJsonHttpMessageConverter处理请求参数的入口如下:

在这里插入图片描述

1)FastJsonHttpMessageConverter#canRead()

判断FastJsonHttpMessageConverter是否可以转换当前ContentType的请求:

  • FastJsonHttpMessageConverter可以转换任意类型的请求。

在这里插入图片描述

2)FastJsonHttpMessageConverter#read()

直接调用FastJson的序列化API将JSON字符串数据系列化为相应的Java Type。

在这里插入图片描述

相关内容

热门资讯

加拿大9月GDP环比增长0.2... 11月28日消息,加拿大9月GDP环比增长0.2%,预期增长0.2%,前值由-0.30%修正为-0....
芝商所:目前芝商所所有市场均已... 11月28日消息,芝商所(CME)称,目前芝商所所有市场均已开放并恢复交易。(广角观察)
纽约黄金期货价格上涨0.3%,... 11月28日消息,纽约黄金期货价格上涨0.3%,报4215.3美元/盎司。(广角观察)
首批科创创业人工智能ETF现一... 11月28日消息,从有关渠道获悉,11月28日首批科创创业人工智能ETF发行首日,永赢中证科创创业人...
德国商用车制造商曼恩计划在本国... 11月28日消息,德国大众集团旗下的商用车制造子公司曼恩主要生产卡车、客车以及柴油发动机。当地时间2...
许继电气:中标国家电网特高压项... 11月28日消息,许继电气公告称,公司中标国家电网有限公司2025年第六十四批采购(特高压项目第四次...
陈春江辞去陕西省副省长职务 11月28日消息,11月28日下午,陕西省第十四届人民代表大会常务委员会第十九次会议举行第二次全体会...
商业航天概念盘初活跃,乾照光电... 11月28日消息,商业航天概念活跃,乾照光电触及20cm涨停,此前顺灏股份一字涨停,新雷能、佳缘科技...
锂电池电解液概念再度走强,海科... 11月28日消息,锂电池电解液概念再度走强,海科新源涨超10%,创历史新高,天力锂能、万润新能、石大...
精神病院炒股炒成上市公司前十大... 红星新闻 近日,上市公司盛通股份(002599.SZ)发布一季报,披露了前十大股东名单,其中一家名为...