前后的交互js数字精度丢失解决,⾦额保留两位⼩数四舍五⼊统⼀解决,⾃定义Json序列化处理。。。
引⼊maven坐标
<!-- mvnrepository/artifact/com./jackson-databind -->
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.6</version>
</dependency>
<!-- mvnrepository/artifact/com./jackson-core -->
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.6</version>
</dependency>
<!-- mvnrepository/artifact/com./jackson-annotations -->
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.6</version>
</dependency>
js数字精度丢失解决,只需要在参数上加上@JsonSerialize(using = ToStringSerializer.class)就⾏了,返回给前端回⾃动将Long转为字符串。
@JsonSerialize(using = ToStringSerializer.class)
private Long customerId;
ToStringSerializer源码:
基于ToStringSerializer的源码,我⾃⼰定义了⾦额保留两位⼩数四舍五⼊的处理类,只需要继承StdSerializer.class,重写serialize():
import java.io.IOException;
import java.math.BigDecimal;
/**
* 保留两位⼩数,四舍五⼊保
*
* @author 向振华
* @date 2022/01/14 11:44
*/
public class RoundHalfUpSerializer extends StdSerializer<BigDecimal> {
protected RoundHalfUpSerializer() {
bigdecimal除法保留小数super(BigDecimal.class);
}
public RoundHalfUpSerializer(Class<?> handledType) {
super(handledType, false);
}
@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider provider) throws IOException { // 这⾥将BigDecimal 保留两位⼩数,四舍五⼊保
gen.writeNumber(value.setScale(2, BigDecimal.ROUND_HALF_UP));
}
}
然后在字段上加上@JsonSerialize(using = RoundHalfUpSerializer.class)
@Data
public class OrderPageVO {
@ApiModelProperty(value = "总应缴⾦额")
@JsonSerialize(using = RoundHalfUpSerializer.class)
private BigDecimal totalPayableAmount;
@ApiModelProperty(value = "总已缴⾦额")
@JsonSerialize(using = RoundHalfUpSerializer.class)
private BigDecimal totalPaidAmount;
}
这样在返回给前端的json⾥⾯就是处理后的数据。
另外⼜扩展⼀下
import org.apachemons.lang3.StringUtils;
import java.io.IOException;
import java.math.BigDecimal;
/**
* 进位法,保留两位⼩数,转字符串
* 可以加在Number类型、数字String类型字段上
*
* @author 向振华
* @date 2022/01/14 11:44
*/
public class RoundUpSerializer extends StdSerializer<Object> {
protected RoundUpSerializer() {
super(Object.class);
}
public RoundUpSerializer(Class<?> handledType) {
super(handledType, false);
}
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (value == null) {
gen.writeString("0.00");
} else if (value instanceof Number) {
gen.writeString(new String()).setScale(2, BigDecimal.ROUND_UP).toString());
} else if (value instanceof String && StringUtils.String())) {
gen.writeString(new String()).setScale(2, BigDecimal.ROUND_UP).toString());
} else {
gen.String());
}
}
}
注意:被转换的字段必须是包装类类型,否则会转换失败。如果字段为null,默认不会序列化,需要加上nullsUsing = RoundUpSerializer.class,例如:
@JsonSerialize(using = RoundUpSerializer.class, nullsUsing = RoundUpSerializer.class)
private BigDecimal advanceBalanceAmount;
另外还有将⽇期格式化的@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
忽略序列化的@JsonIgnore
⼤量注解详见com.fasterxml.jackson.annotation源码。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论