springboot中json對象中對Long類型和String類型相互轉(zhuǎn)換
與前端聯(lián)調(diào)接口時,后端一些字段設(shè)計為Long類型,這樣就有可能導(dǎo)致前端缺失精度,這時候我們就需要將Long類型返回給前端時做數(shù)據(jù)類型轉(zhuǎn)換,在使用springboot開發(fā)時,默認(rèn)的json序列化和反序列化使用的是Jackson,所以可以有兩種處理方式。
第一種是直接在字段上面添加注解,指定序列化字段類型:
@JsonSerialize(using = ToStringSerializer.class) private Long id;
一般在項目中這樣配置可以解決單個實體類中的字段序列化,如果項目中的實體類非常多,這種配置方式就會顯得非常麻煩。所以我們可以調(diào)整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處理數(shù)據(jù)時精度缺失
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) // 駝峰字段轉(zhuǎn)下劃線字段
.serializationInclusion(Include.NON_NULL).serializersByType(serializers)
.deserializersByType(deserializers).simpleDateFormat("yyyy-MM-dd HH:mm:ss").timeZone("GMT+8")
.locale(Locale.SIMPLIFIED_CHINESE);
}
}
第二種方式可以避免每個實體類中都配置序列化方式,但是這種方式也限制了項目的靈活性,它會對全局?jǐn)?shù)據(jù)都產(chǎn)生影響,對比第一種它顯然不夠靈活。
到此這篇關(guān)于springboot中json對象中對Long類型和String類型相互轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)springboot Long類型和String類型轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Spring Boot Mybatis 搞反向工程的步驟
這篇文章主要介紹了使用Spring Boot Mybatis 搞反向工程的步驟,幫助大家更好的理解和使用spring boot框架,感興趣的朋友可以了解下2021-01-01
Spring Boot項目利用Redis實現(xiàn)session管理實例
本篇文章主要介紹了Spring Boot項目利用Redis實現(xiàn)session管理實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
解決Intellij IDEA 使用Spring-boot-devTools無效的問題
下面小編就為大家?guī)硪黄鉀QIntellij IDEA 使用Spring-boot-devTools無效的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
Java面試Logback打印日志如何獲取當(dāng)前方法名稱題解
這篇文章主要為大家介紹了Java面試Logback打印日志如何獲取當(dāng)前方法名稱題解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
Java農(nóng)夫過河問題的繼承與多態(tài)實現(xiàn)詳解
這篇文章主要介紹了Java農(nóng)夫過河問題的繼承與多態(tài)實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01

