使用spring boot + vue做项目时,当后端接口传Long类型数据给前端当时候,精度丢失,导致数据不准确。

解决方案:

在JavaBean上之间加上下面的注解  (spring boot默认使用Jackson类库),对象序列化成JSON时,将Long转成String

import com.fasterxml.jackson.databind.annotation.JsonSerialize; 

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; 



@JsonSerialize(using = ToStringSerializer.class) 

Long id

附:Jackson的ToStringSerializer源码

public class ToStringSerializer extends StdSerializer<Object> {
    public static final ToStringSerializer instance = new ToStringSerializer();

    public ToStringSerializer() {
        super(Object.class);
    }

    public ToStringSerializer(Class<?> handledType) {
        super(handledType, false);
    }

    public boolean isEmpty(SerializerProvider prov, Object value) {
        return value.toString().isEmpty();
    }

    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeString(value.toString());
    }

    public void serializeWithType(Object value, JsonGenerator g, SerializerProvider provider, TypeSerializer typeSer) throws IOException {
        WritableTypeId typeIdDef = typeSer.writeTypePrefix(g, typeSer.typeId(value, JsonToken.VALUE_STRING));
        this.serialize(value, g, provider);
        typeSer.writeTypeSuffix(g, typeIdDef);
    }

    public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException {
        return this.createSchemaNode("string", true);
    }

    public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException {
        this.visitStringFormat(visitor, typeHint);
    }
}

若想自定义类型可以重写 JsonSerializer serialize方法。

如Date转成Long类型的时间戳

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
 
import java.io.IOException;
import java.util.Date;
 
public class CustomSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        jsonGenerator.writeNumber(date.getTime()/1000);
    }
}





// 创建时间
    @JsonSerialize(using = CustomSerializer .class)
    private Date createTime;

---------------------

本文来自 翻墨 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_37749185/article/details/81131414?utm_source=copy 

2、如果使用FastJson,在JavaBean中使用FastJson注解
 

import com.alibaba.fastjson.annotation.JSONField;

import com.alibaba.fastjson.serializer.ToStringSerializer; 



@JSONField(serializeUsing= ToStringSerializer.class) 

private Long id; 

附:FastJson的ToStringSerializer源码

public class ToStringSerializer implements ObjectSerializer {
    public static final ToStringSerializer instance = new ToStringSerializer();

    public ToStringSerializer() {
    }

    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        SerializeWriter out = serializer.out;
        if (object == null) {
            out.writeNull();
        } else {
            String strVal = object.toString();
            out.writeString(strVal);
        }
    }
}

如果想扩展该注解,需要通过实现ObjectSerializer接口来完成

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Jackson类库有很多注解,可以让我们快速建立Java和JSON的关系,详细可以参考:https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations

有关FastJson @JSONField的使用,可以参考:https://www.w3cschool.cn/fastjson/fastjson-jsonfield.html

 

Logo

前往低代码交流专区

更多推荐