当前位置:网站首页>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
其他链接:
边栏推荐
- Slf4j log facade
- 【R】 [density clustering, hierarchical clustering, expectation maximization clustering]
- Brief introduction to mvcc
- In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
- Servlet
- Will Huawei be the next one to fall
- Flink code is written like this. It's strange that the window can be triggered (bad programming habits)
- Flick SQL knows why (10): everyone uses accumulate window to calculate cumulative indicators
- [Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]
- 已解决(机器学习中查看数据信息报错)AttributeError: target_names
猜你喜欢
When we are doing flow batch integration, what are we doing?
stm32和电机开发(从mcu到架构设计)
高效能人士的七个习惯
IDEA 全文搜索快捷键Ctr+Shift+F失效问题
MySQL constraints
Mysql database basic operation - regular expression
2022-02-14 analysis of the startup and request processing process of the incluxdb cluster Coordinator
Flink SQL knows why (16): dlink, a powerful tool for developing enterprises with Flink SQL
Annotation and reflection
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 6 exercises]
随机推荐
CVPR 2022 image restoration paper
35道MySQL面试必问题图解,这样也太好理解了吧
Flink SQL knows why (12): is it difficult to join streams? (top)
【R】【密度聚类、层次聚类、期望最大化聚类】
【R】 [density clustering, hierarchical clustering, expectation maximization clustering]
Leetcode234 palindrome linked list
Setting up Oracle datagurd environment
【習題五】【數據庫原理】
Brief introduction to mvcc
When we are doing flow batch integration, what are we doing?
Sword finger offer14 the easiest way to cut rope
The difference between session and cookie
IDEA 全文搜索快捷键Ctr+Shift+F失效问题
【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 6 exercises]
Mysqlbetween implementation selects the data range between two values
File uploading and email sending
[colab] [7 methods of using external data]
MySQL_ JDBC
PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?