本文主要是介绍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;
}
测试
@Autowiredprotected ObjectMapper objectMapper;@Testpublic 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<Number> {private static final long serialVersionUID = -4714217911586331902L;public MyNumberSerializer() {super(Number.class);}@Overridepublic 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),可使用全局配置,如下:
@Autowiredprotected ObjectMapper objectMapper;@Beforepublic void beforeTest() {// 全局配置SimpleModule module = new SimpleModule();module.addSerializer(Long.class, new MyNumberSerializer());module.addSerializer(BigDecimal.class, new MyNumberSerializer());objectMapper.registerModule(module);}@Testpublic 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));}
这篇关于SpringBoot使用ObjectMapper之Long和BigDemical类型的属性字符串处理,防止前端丢失数值精度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!