search

Home  >  Q&A  >  body text

java - spring boot返回json数据时long类型数据失真

"id" : 24787807536939046,
"acId" : 24768209282400257,
"insertTime" : 1476957265000,
"lastUpdateTime" : 1476957265000,

这里返回的id,acid因为是long类型,如果数值太大在js里会失真,怎么在返回的时候让24787807536939046为string类型,通过注解

大家讲道理大家讲道理2885 days ago858

reply all(4)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 10:29:00

    Change the type of id to String, and do the conversion between Long and String when processing.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:29:00

    Similar to this question about portal.

    If it doesn’t meet your needs, you need to customize a Gson serialization method yourself.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:29:00

    Customized ObjectMapper will automatically convert the Long type into string and send it to the front end

    public class CustomObjectMapper extends ObjectMapper {
    
        private static final long serialVersionUID = 3223645203459453114L;
    
        /**
         * 构造函数
         */
        public CustomObjectMapper() {
            super();
            SimpleModule simpleModule = new SimpleModule();
            simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
            simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
            registerModule(simpleModule);
    }

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:29:00

    (1) If the acId type can be changed, change it to string.
    If the acId type implementation cannot be changed. (2) Method 1, redefine a javabean, but change the acId type to string.
    (3) Method two, process the gson string again and add "" to both sides of 24768209282400257.

    reply
    0
  • Cancelreply