SpringBoot Jackson

1. SpringBoot JSON工具包默認是Jackson,只須要引入spring-boot-starter-web依賴包,自動引入相應依賴包: html

<dependency>

        <groupId>com.fasterxml.jackson.core</groupId>

        <artifactId>jackson-databind</artifactId> -->數據綁定依賴於下面兩個包

        <version>2.8.7</version>

</dependency>

<dependency>

        <groupId>com.fasterxml.jackson.core</groupId>

        <artifactId>jackson-annotations</artifactId> -->註解包

        <version>2.8.0</version>

</dependency>

<dependency>

        <groupId>com.fasterxml.jackson.core</groupId>

        <artifactId>jackson-core</artifactId> -->核心包

        <version>2.8.7</version>

</dependency>

2. Jackson兩種配置方式web

  A. application.properties文件spring

# 日期格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
# 日期時區
spring.jackson.time-zone=GMT+8
# 返回值null不顯示
spring.jackson.default-property-inclusion=non_null

  B. bean配置json

package com.ruhuanxingyun.config;

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;

@Configuration
public class JacksonConfig {

@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// 返回值過濾null或""值
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY).setSerializationInclusion(JsonInclude.Include.NON_NULL);

return objectMapper;
}

}

 

可參考:SpringBoot Jackson配置方式
app

    SpringBoot 返回值XMLspring-boot

    jackson更多瞭解工具

相關文章
相關標籤/搜索