springboot中用fastjson處理返回值為null的屬性值
我們先來(lái)看代碼:
@Configurationpublic 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è)可以在返回的信息中,假如說(shuō)有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處理返回值問(wèn)題詳解內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. Spring如何使用xml創(chuàng)建bean對(duì)象3. python實(shí)現(xiàn)在內(nèi)存中讀寫str和二進(jìn)制數(shù)據(jù)代碼4. python實(shí)現(xiàn)PolynomialFeatures多項(xiàng)式的方法5. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)6. python 利用toapi庫(kù)自動(dòng)生成api7. Java程序的編碼規(guī)范(6)8. Android Studio設(shè)置顏色拾色器工具Color Picker教程9. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法10. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例
