springboot中json对象中对Long类型和String类型相互转换
作者:腊笔不小新xingo
与前端联调接口时,后端一些字段设计为Long类型,这样就有可能导致前端缺失精度,这时候我们就需要将Long类型返回给前端时做数据类型转换,本文主要介绍了springboot中json对象中对Long类型和String类型相互转换,感兴趣的可以了解一下
与前端联调接口时,后端一些字段设计为Long类型,这样就有可能导致前端缺失精度,这时候我们就需要将Long类型返回给前端时做数据类型转换,在使用springboot开发时,默认的json序列化和反序列化使用的是Jackson,所以可以有两种处理方式。
第一种是直接在字段上面添加注解,指定序列化字段类型:
@JsonSerialize(using = ToStringSerializer.class) private Long id;
一般在项目中这样配置可以解决单个实体类中的字段序列化,如果项目中的实体类非常多,这种配置方式就会显得非常麻烦。所以我们可以调整Jackson的配置,在全局指定序列化配置:
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; @Configuration(proxyBeanMethods = false) @ConditionalOnClass(ObjectMapper.class) @AutoConfigureBefore(JacksonAutoConfiguration.class) public class JacksonConfiguration { private final Map<Class<?>, JsonSerializer<?>> serializers = new LinkedHashMap<>(5); private final Map<Class<?>, JsonDeserializer<?>> deserializers = new LinkedHashMap<>(3); @Bean @Order(Ordered.HIGHEST_PRECEDENCE) Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { // 序列化时对Long类型进行处理,避免前端js处理数据时精度缺失 serializers.put(Long.class, ToStringSerializer.instance); serializers.put(Long.TYPE, ToStringSerializer.instance); // java8日期处理 serializers.put(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); serializers.put(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); serializers.put(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); deserializers.put(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); deserializers.put(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); deserializers.put(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); return customizer -> customizer .featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS, SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS, DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) //.propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE) // 驼峰字段转下划线字段 .serializationInclusion(Include.NON_NULL).serializersByType(serializers) .deserializersByType(deserializers).simpleDateFormat("yyyy-MM-dd HH:mm:ss").timeZone("GMT+8") .locale(Locale.SIMPLIFIED_CHINESE); } }
第二种方式可以避免每个实体类中都配置序列化方式,但是这种方式也限制了项目的灵活性,它会对全局数据都产生影响,对比第一种它显然不够灵活。
到此这篇关于springboot中json对象中对Long类型和String类型相互转换的文章就介绍到这了,更多相关springboot Long类型和String类型转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!