zoukankan      html  css  js  c++  java
  • java web项目服务端返回json结果时过滤字段为null的属性

    1.全局配置返回的对象排除为空和null的属性
    import
    com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; /** * @Description: 全局设置空值和null值不序列化 * @Author: lg * @CreateDate: 2020/1/17 */ @Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); return objectMapper; } }

     2.在返回的实体类上加注解实现

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class LogStatistics{}
     
  • 相关阅读:
    持久化类的三种状态
    Hibernate持久化类规则
    JSP之Bean
    JSP动作标签
    JSP九大内置对象
    Jsp指令
    JSTL标签语言
    JSP之EL表达式
    Java 中的 Characters
    汇编基本语法
  • 原文地址:https://www.cnblogs.com/lgjava/p/13915947.html
Copyright © 2011-2022 走看看