【SpringBoot】85、SpringBoot中Boolean类型数据转0/1返回序列化配置

在 SpringBoot 中,前端传参数 0,1,后端可自动解析为 boolean 类型,但后端返回前端 boolean 类型时,却无法自动转换为 0,1,所以我们需要自定义序列化配置,将 boolean 类型转化为 0,1

1、类型对应

  • boolean 类型有
false,true
  • 对应的 int 类型
0,1

2、序列化配置

import com.fasterxml.jackson.core.JsonGenera
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot使用Fastjson的配置方法如下: 1. 添加Fastjson依赖 在pom.xml文件添加Fastjson依赖: ``` <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> ``` 2. 配置Fastjson 在Spring Boot配置添加以下配置: ``` @Configuration public class FastjsonConfig { @Bean public HttpMessageConverters fastjsonHttpMessageConverters() { // 创建Fastjson消息换器 FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); // 创建配置类 FastJsonConfig config = new FastJsonConfig(); // 配置序列化策略 config.setSerializerFeatures( SerializerFeature.WriteMapNullValue, // 输出空置字段 SerializerFeature.WriteNullListAsEmpty, // list字段如果为null,输出为[],而不是null SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null,输出为0,而不是null SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null,输出为false,而不是null SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null,输出为"",而不是null ); converter.setFastJsonConfig(config); HttpMessageConverter<?> converter1 = converter; return new HttpMessageConverters(converter1); } } ``` 3. 使用Fastjson 在Controller使用Fastjson进行序列化和反序列化: ``` @RestController public class UserController { @PostMapping("/user") public User addUser(@RequestBody User user) { // ... return user; } @GetMapping("/user/{id}") public User getUser(@PathVariable Long id) { // ... return user; } } ``` 以上就是Spring Boot使用Fastjson的配置方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Asurplus

学如逆水行舟,不进则退

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值