您好,登录后才能下订单哦!
这篇文章主要介绍“SpringBoot如何解决BigDecimal传到前端后精度丢失问题”,在日常操作中,相信很多人在SpringBoot如何解决BigDecimal传到前端后精度丢失问题问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringBoot如何解决BigDecimal传到前端后精度丢失问题”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
Controller
package com.knife.controller; import com.knife.entity.UserVO; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.math.BigDecimal; @RestController @RequestMapping("user") public class UserController { @GetMapping("save") public UserVO save(BigDecimal amount) { UserVO userVO = new UserVO(); userVO.setId(1L); userVO.setUsername("Tony"); userVO.setAmount(amount); return userVO; } }
Entity
package com.knife.entity; import lombok.Data; import java.math.BigDecimal; @Data public class UserVO { private Long id; private String username; private BigDecimal amount; }
测试
访问:http://localhost:8080/user/save?amount=12345671234567.1234
结果
场景描述
实际项目中前端会这样处理:调用后端接口获得JSON格式的响应字符串,然后将JSON字符串解析为JavaScript对象(用于展示到对应的位置、方便计算等)。
前端调后端的写接口(增删改)时,会将JavaScript对象序列化为JSON格式的字符串,然后将其作为参数请求后端接口。
实例1:精度丢失
const json = '{"id": 1, "name": "Tony", "amount": 12345671234567.12345}'; const obj = JSON.parse(json); console.log(obj.amount); // 12345671234567.123 console.log(JSON.stringify(obj)); // {"id":1,"name":"Tony","amount":12345671234567.123}
可以看到,在将json字符串转为JavaScript对象后,“amount” 丢失了精度。
实例2:丢失小数位
const json = '{"id": 1, "name": "Tony", "amount": 12345671234567.00000}'; const obj = JSON.parse(json); console.log(obj.amount); // 12345671234567 console.log(JSON.stringify(obj)); // {"id":1,"name":"Tony","amount":12345671234567}
可以看到,在将json字符串转为JavaScript对象后,“amount” 丢失了小数。
其他示例
const json = '{"id": 1, "name": "Tony", "amount": 12345671234567.12345}'; const obj = JSON.parse(json); console.log(obj.amount); // 12345671234567.123 const json = '{"id": 1, "name": "Tony", "amount": 123456712345678.12345}'; const obj = JSON.parse(json); console.log(obj.amount); // 123456712345678.12 const json = '{"id": 1, "name": "Tony", "amount": 98765432198765.12345}'; const obj = JSON.parse(json); console.log(obj.amount); // 98765432198765.12 const json = '{"id": 1, "name": "Tony", "amount": 987654321987654321.12345}'; const obj = JSON.parse(json); console.log(obj.amount); // 987654321987654300
1.范围没有限制,可以认为无限大、无限小
2.可以通过如下代码验证:
package com.example.a; import java.math.BigDecimal; public class Demo { public static void main(String[] args) { BigDecimal bigDecimal = new BigDecimal( "1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890" + ".123456789" ); System.out.println(bigDecimal); } }
执行结果:
12345678901234567890123456789012345678901234567890123456789012345678901234567890.123456789
把BigDecimal的序列化值改成字符串类型即可。
法1:ToStringSerializer
配置类
package com.knife.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import java.math.BigDecimal; @Configuration public class JacksonConfig { @Bean public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); // 全局配置序列化返回 JSON 处理 SimpleModule simpleModule = new SimpleModule(); // 将使用String来序列化BigDecimal类型 simpleModule.addSerializer(BigDecimal.class, ToStringSerializer.instance); objectMapper.registerModule(simpleModule); return objectMapper; } }
测试
访问:http://localhost:8080/user/save?amount=12345671234567.1234
结果:
法2:自定义序列化
自定义序列化器
package com.knife.config; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.annotation.JacksonStdImpl; import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; @JacksonStdImpl class BigDecimalToStringSerializer extends ToStringSerializer { public final static BigDecimalToStringSerializer instance = new BigDecimalToStringSerializer(); public BigDecimalToStringSerializer() { super(Object.class); } public BigDecimalToStringSerializer(Class<?> handledType) { super(handledType); } @Override public boolean isEmpty(SerializerProvider prov, Object value) { if (value == null) { return true; } String str = ((BigDecimal) value).stripTrailingZeros().toPlainString(); return str.isEmpty(); } @Override public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException { gen.writeString(((BigDecimal) value).stripTrailingZeros().toPlainString()); // 如果要求所有BigDecimal保留两位小数,可以这么写: // gen.writeString(((BigDecimal) value).setScale(2, RoundingMode.HALF_UP) // .stripTrailingZeros().toPlainString()); } @Override public void serializeWithType(Object value, JsonGenerator gen, SerializerProvider provider, TypeSerializer typeSer) throws IOException { // no type info, just regular serialization serialize(value, gen, provider); } }
配置类
package com.knife.config; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import java.math.BigDecimal; @Configuration public class JacksonConfig { @Bean public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); // 全局配置序列化返回 JSON 处理 SimpleModule simpleModule = new SimpleModule(); // 将使用String来序列化BigDecimal类型 simpleModule.addSerializer(BigDecimal.class, BigDecimalToStringSerializer.instance); objectMapper.registerModule(simpleModule); return objectMapper; } }
测试
访问:http://localhost:8080/user/save?amount=12345671234567.1234
结果:
法1:@JsonSerialize
在相应字段上加此注解:
@JsonSerialize(using= ToStringSerializer.class)
示例
package com.knife.entity; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import lombok.Data; import java.math.BigDecimal; @Data public class UserVO { private Long id; private String username; @JsonSerialize(using= ToStringSerializer.class) private BigDecimal amount; }
测试
访问:http://localhost:8080/user/save?amount=12345671234567.1234
结果:
法2:@JsonFormat
在相应字段上加此注解:
@JsonFormat(shape = JsonFormat.Shape.STRING)
示例
package com.knife.entity; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.math.BigDecimal; @Data public class UserVO { private Long id; private String username; @JsonFormat(shape = JsonFormat.Shape.STRING) private BigDecimal amount; }
测试
访问:http://localhost:8080/user/save?amount=12345671234567.1234
结果:
到此,关于“SpringBoot如何解决BigDecimal传到前端后精度丢失问题”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。