springboot+jersay環境下解決long類型到前臺精度丟失的問題

項目採用先後端分離的方式開發,後臺接口開發好後經過postman也測試經過了,可是集成前端的時候發出,後臺LONG類型的數據到了前臺後數度丟失了:前端

後臺數據:spring

前臺:json

最後一位精度丟失。後端

主要緣由多是js內部的數據表示問題,具體緣由還不太瞭解。app

解決方法:前後端分離

將long類型的數據轉爲string類型。post

可經過自定義spring ObjectMapper來統一實現:測試

@EnableTransactionManagement
@MapperScan("com.makeronly.*.repo")
@SpringBootApplication
public 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);
    }

主要是標紅的這幾句話實現。spa

相關文章
相關標籤/搜索