您的位置:首頁技術文章
文章詳情頁

springboot中用fastjson處理返回值為null的屬性值

瀏覽:59日期:2023-05-29 17:15:42

我們先來看代碼:

@Configurationpublic class WebMvcConfig extends WebMvcConfigurationSupport { public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() { FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); //todo 這里進行配置,空和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; }}

配置上這個可以在返回的信息中,假如說有null字段的時候,前端不會進行顯示這種信息

知識點擴展:

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的注解,比如

springboot中用fastjson處理返回值為null的屬性值

到此這篇關于springboot中用fastjson處理返回值為null的屬性值的文章就介紹到這了,更多相關springboot中用fastjson處理返回值問題詳解內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
国产综合久久一区二区三区