SpringBoot使用ObjectMapper之Long和BigDemical类型的属性字符串处理,防止前端丢失数值精度

SpringBoot使用ObjectMapper之Long和BigDemical类型的属性字符串处理,防止前端丢失数值精度!

方式一:注解

使用注解 @JsonFormat(shape = JsonFormat.Shape.STRING),如下:

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Demo implements Serializable {
    private static final long serialVersionUID = -2148559076064821626L;

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long id;

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private BigDecimal price;
}

测试

    @Autowired
    protected ObjectMapper objectMapper;

    @Test
    public void objectMapperTest() throws Exception {
        Demo demo = new Demo(Long.MAX_VALUE, new BigDecimal("12.34"));
        System.out.println(objectMapper.writeValueAsString(demo));
        demo = objectMapper.readValue("{\"id\":\"9223372036854775807\",\"price\":123.4}", Demo.class);
        System.out.println(objectMapper.writeValueAsString(demo));
    }

输出

{"id":"9223372036854775807","price":"12.34"}
{"id":"9223372036854775807","price":"123.4"}

方式二:自定义序列化转换器

自定义序列化转换器代码

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

public class MyNumberSerializer extends StdSerializer {
    private static final long serialVersionUID = -4714217911586331902L;

    public MyNumberSerializer() {
        super(Number.class);
    }

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

使用自定义序列化转换器

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Demo implements Serializable {
    private static final long serialVersionUID = -2148559076064821626L;

    @JsonSerialize(using = MyNumberSerializer.class)
    private Long id;

    @JsonSerialize(using = MyNumberSerializer.class)
    private BigDecimal price;
}

想在所有实体类中生效而避免添加注解:@JsonSerialize(using = MyNumberSerializer.class),可使用全局配置,如下:

    @Autowired
    protected ObjectMapper objectMapper;

    @Before
    public void beforeTest() {
        // 全局配置
        SimpleModule module = new SimpleModule();
        module.addSerializer(Long.class, new MyNumberSerializer());
        module.addSerializer(BigDecimal.class, new MyNumberSerializer());
        objectMapper.registerModule(module);
    }

    @Test
    public void objectMapperTest() throws Exception {
        
        Demo demo = new Demo(Long.MAX_VALUE, new BigDecimal("12.34"));
        System.out.println(objectMapper.writeValueAsString(demo));
        demo = objectMapper.readValue("{\"id\":9223372036854775807,\"price\":\"123.4\"}", Demo.class);
        System.out.println(objectMapper.writeValueAsString(demo));
    }

你可能感兴趣的:(spring,boot,ObjectMapper)