当前位置:网站首页>json序列化时案例总结
json序列化时案例总结
2022-07-03 12:36:00 【伏加特遇上西柚】
1、 @JsonSerialize
@JsonSerialize@JsonSerialize注解,主要应用于数据转换,该注解作用在该属性的getter()方法上。
案例一
场景: id为Long类型时,数字长度超过19位传到前端就变了值 。精度丢失了。
解决办法:将Long类型转换成String类型,解决精度丢失。
注意:被转换的字段必须是包装类类型,否则会转换失败。
@JsonSerialize(using = ToStringSerializer.class)
private Long parentId; //转化成功
@JsonSerialize(using = ToStringSerializer.class)
private long parentId; //转化失败
案例二
场景:需要保留金额两位小数,四舍五入
解决办法:定义方法
@JsonSerialize(using = CustomDecimalSerialize.class)
private BigDecimal totalCom;
CustomDecimalSerialize
public class CustomDecimalSerialize extends JsonSerializer<BigDecimal> {
private DecimalFormat df = new DecimalFormat("0.00");
@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
if (value != null) {
df.setRoundingMode(RoundingMode.HALF_UP);
gen.writeString(df.format(value));
}
}
}
2、 @JsonProperty
@JsonProperty("xxx")用在属性上面,在序列化和反序列化时都将该属性识别为xxx
//序列化时将DATA_SOURCE映射成dataSource
@JsonProperty(value = "DATA_SOURCE")
private String dataSource;
3、@DateTimeFormat和@JsonFormat
其他链接:
边栏推荐
- Oracle memory management
- 【R】 [density clustering, hierarchical clustering, expectation maximization clustering]
- Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
- Idea full text search shortcut ctr+shift+f failure problem
- Logback 日志框架
- 2022-02-11 heap sorting and recursion
- Brief introduction to mvcc
- 2022-01-27 redis cluster technology research
- Sword finger offer 16 Integer power of numeric value
- 解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.
猜你喜欢

sitesCMS v3.1.0发布,上线微信小程序

Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial

PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?

Flink SQL knows why (XI): weight removal is not only count distinct, but also powerful duplication
![[colab] [7 methods of using external data]](/img/cf/07236c2887c781580e6f402f68608a.png)
[colab] [7 methods of using external data]

mysql更新时条件为一查询

Idea full text search shortcut ctr+shift+f failure problem

01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
![[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)](/img/45/c2d7934b886d8090373ca9e6e23c97.gif)
[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)

Flick SQL knows why (10): everyone uses accumulate window to calculate cumulative indicators
随机推荐
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
Sword finger offer 16 Integer power of numeric value
When the R language output rmarkdown is in other formats (such as PDF), an error is reported, latex failed to compile stocks Tex. solution
IDEA 全文搜索快捷键Ctr+Shift+F失效问题
Tencent cloud tdsql database delivery and operation and maintenance Junior Engineer - some questions of Tencent cloud cloudlite certification (TCA) examination
C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep271
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
Deeply understand the mvcc mechanism of MySQL
有限状态机FSM
[Exercice 5] [principe de la base de données]
18W word Flink SQL God Road manual, born in the sky
正则表达式
Logback 日志框架
When we are doing flow batch integration, what are we doing?
sitesCMS v3.0.2发布,升级JFinal等依赖
2022-01-27 use liquibase to manage MySQL execution version
My creation anniversary: the fifth anniversary
February 14, 2022, incluxdb survey - mind map
人身变声器的原理
C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous