项目采用前后端分离的方式开发,后台接口开发好后通过postman也测试通过了,但是集成前端的时候发出,后台LONG类型的数据到了前台后数度丢失了:
后台数据:
前台:
最后一位精度丢失。
主要原因可能是js内部的数据表示问题,具体原因还不太了解。
解决方法:
将long类型的数据转为string类型。
可通过自定义spring ObjectMapper来统一实现:
@EnableTransactionManagement@MapperScan("com.makeronly.*.repo")@SpringBootApplicationpublic class Application{ /** * 防止json时出现错误FAIL_ON_EMPTY_BEANS * @return */ @Bean public ObjectMapper objectMapper() { SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(Long.class, ToStringSerializer.instance); simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); ObjectMapper myObjectMapper = new ObjectMapper(); myObjectMapper.registerModule(simpleModule); myObjectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); return myObjectMapper; } public static void main(String[] args){ SpringApplication.run(Application.class, args); }
主要是标红的这几句话实现。