springboot中用fastjson处理返回值为null的属性值

我们先来看代码:

@Configuration
public 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处理返回值问题详解内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • springboot实现FastJson解析json数据的方法

    最近在研究springboot实现FastJson解析json数据的方法,那么今天也算个学习笔记吧! 添加jar包: <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> 两种方式启动加载类: 第一种继承WebMvcConfigur

  • SpringBoot如何使用Fastjson解析Json数据

    方法一: 1.在pom.xml文件下添加依赖包 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> 2.修改启动文件 package myshop; import java.util.List; import org.spring

  • SpringBoot整合FastJson过程解析

    这篇文章主要介绍了SpringBoot整合FastJson过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一.Maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.33</version> </dependency&

  • Spring Boot使用FastJson解析JSON数据的方法

    个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析呢? 1.引入fastjson依赖库: <!--添加fastjson解析JSON数据--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <versio

  • SpringBoot Redis配置Fastjson进行序列化和反序列化实现

    FastJson是阿里开源的一个高性能的JSON框架,FastJson数据处理速度快,无论序列化(把JavaBean对象转化成Json格式的字符串)和反序列化(把JSON格式的字符串转化为Java Bean对象),都是当之无愧的fast:功能强大(支持普通JDK类,包括javaBean, Collection, Date 或者enum):零依赖(没有依赖其他的任何类库). 1.写一个自定义序列化类 /** * 自定义序列化类 * @param <T> */ public class FastJ

  • 使用SpringBoot+OkHttp+fastjson实现Github的OAuth第三方登录

    一.在GitHub上创建一个OAuth 二.OAuth的原理 Spring官方文档 三.OkHttp的使用 OkHttp官方网站 1.Post 代码示例 //官方文档: public static final MediaType JSON // = MediaType.get("application/json; charset=utf-8"); MediaType mediaType = MediaType.get("application/json; charset=ut

  • springboot中用fastjson处理返回值为null的属性值

    我们先来看代码: @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() { FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter()

  • SpringBoot项目中处理返回json的null值(springboot项目为例)

    在后端数据接口项目开发中,经常遇到返回的数据中有null值,导致前端需要进行判断处理,否则容易出现undefined的情况,如何便捷的将null值转换为空字符串? 以SpringBoot项目为例,SSM同理. 1.新建配置类(JsonConfig.java) import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.f

  • Java如何取掉json数据中值为null的属性字段

    目录 取掉json数据中值为null的属性字段 用注解的方式放在标记类或者属性 或者在配置文件中添加全局配置 json返回隐藏null值的字段 取掉json数据中值为null的属性字段 @JsonInclude(JsonInclude.Include.NON_NULL)  集成于Springboot2.0中此方法的配置意在实体类与json互转的时候 属性值为null的不参与序列化. 使用时: 用注解的方式放在标记类或者属性 或者在配置文件中添加全局配置 jackson.default-prope

  • jquery修改属性值实例代码(设置属性值)

    设置属性 - attr() jQuery attr() 方法也用于设置/改变属性值. 下面的例子演示如何改变(设置)链接中 href 属性的值: 实例 复制代码 代码如下: $("button").click(function(){$("#keleyi").attr("href","http://www.jb51.net");}); attr() 方法也允许您同时设置多个属性.下面的例子演示如何同时设置 href 和 titl

  • spring boot 下对JSON返回值去除null和空字段操作

    在开发过程中,我们需要统一返回前端json格式的数据,但有些接口的返回值存在 null或者""这种没有意义的字段. 不仅影响理解,还浪费带宽,这时我们可以统一做一下处理,不返回空字段,或者把NULL转成"",spring 内置的json处理框架是Jackson.我们可以对它配置一下达到目的 直接看代码,很简单. /** * 〈返回json空值去掉null和""〉 〈功能详细描述〉 * * @author gogym * @version 2017

  • java 字段值为null,不返回该字段的问题

    目录 java字段值为null,不返回该字段 类上打注解 不让null值返回前端 第一种方法 第二种方法 java字段值为null,不返回该字段 类上打注解 @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) public class GetSalaryInfoResponse {     private String payDate; } 不让null值返回前端 场景: 有时候我们返回给前端的数据是null的,而这些为null的

  • springMVC利用FastJson接口返回json数据相关配置详解

    一直使用的是FastJson,感觉还不错,很方便.看了一段别人的分析,觉得很有道理. 为什么要使用Fastjson,其实原因不需要太多,喜欢就行. 我之所以要替换掉Jackson最主要的原因是Jackson在处理对象之前的循环嵌套关系时不便. ps:什么是对象间的循环嵌套?比如A有一个List,B对象里又有一个A对象,当然返回A对象的Json字符串时,如果是 Jackson就会发生异常,因为Jackson天生不具备处理这种关系的能力,而Fastjson正好具备了这种能力(另,如果你用的是 Jac

  • springboot项目中jackson-序列化-处理 NULL教程

    在项目中有事需要对值为NULL的对象中Field不做序列化输入配置方式如下: [配置类型]: 源码包中的枚举类: public static enum Include { ALWAYS, NON_NULL, NON_ABSENT, NON_EMPTY, NON_DEFAULT, USE_DEFAULTS; private Include() { } } Include.Include.ALWAYS 默认 Include.NON_DEFAULT 属性为默认值不序列化 Include.NON_EMP

随机推荐