springboot中用fastjson處理返回值為null的屬性值
我們先來看代碼:
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//todo 這里進(jìn)行配置,空和null,不返回
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
SerializeConfig serializeConfig = SerializeConfig.globalInstance;
serializeConfig.put(LocalDateTime.class, LocalDateTimeSerializer.instance);
fastJsonConfig.setSerializeConfig(serializeConfig);
List<MediaType> mediaTypeList = new ArrayList<>();
mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
mediaTypeList.add(MediaType.APPLICATION_JSON);
fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypeList);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
return fastJsonHttpMessageConverter;
}
}
配置上這個(gè)可以在返回的信息中,假如說有null字段的時(shí)候,前端不會(huì)進(jìn)行顯示這種信息
知識(shí)點(diǎn)擴(kuò)展:
springboot中用fastjson處理返回值為null的屬性值
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastJsonConfig.setDateFormat("yyyy-MM-dd");
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
然后就可以在返回的DTO中使用fastjson的注解,比如

到此這篇關(guān)于springboot中用fastjson處理返回值為null的屬性值的文章就介紹到這了,更多相關(guān)springboot中用fastjson處理返回值問題詳解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot?@Scheduled?Cron表達(dá)式使用方式
這篇文章主要介紹了SpringBoot?@Scheduled?Cron表達(dá)式使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
spring cloud oauth2 feign 遇到的坑及解決
這篇文章主要介紹了spring cloud oauth2 feign 遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
idea如何在service窗口中顯示多個(gè)服務(wù)
這篇文章主要介紹了idea如何在service窗口中顯示多個(gè)服務(wù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Netty分布式從recycler對(duì)象回收站獲取對(duì)象過程剖析
這篇文章主要為大家介紹了Netty分布式從recycler獲取對(duì)象的過程源碼剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
Spring實(shí)戰(zhàn)之緩存使用condition操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之緩存使用condition操作,結(jié)合實(shí)例形式分析了Spring緩存使用condition具體配置、屬性、領(lǐng)域模型等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-01-01

