SpringBoot中Jackson ObjectMapper应用及说明

 更新时间:2026年05月28日 09:33:46   作者:JAVA@架构  
SpringBoot支持支持Jackson作为默认的JSON映射库,提供便捷的对象与JSON转换功能,文章详细介绍了ObjectMapper的配置与使用,以及SpringBoot中Jackson的多种自定义配置方法

Spring Boot支持与三种JSON mapping库集成:Gson、Jackson和JSON-B。

Jackson是首选和默认的。

Jackson是spring-boot-starter-json的一部分,spring-boot-starter-web中包含spring-boot-starter-json。也就是说,当项目中引入spring-boot-starter-web后会自动引入spring-boot-starter-json。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

ObjectMapper

ObjectMapper是jackson-databind包中的一个类,提供读写JSON的功能,可以方便的进行对象和JSON转换:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public final class JsonUtil {
    private static ObjectMapper mapper = new ObjectMapper();
    private JsonUtil() {
    }
    /**
     * Serialize any Java value as a String.
     */
    public static String generate(Object object) throws JsonProcessingException {
        return mapper.writeValueAsString(object);
    }
    /**
     * Deserialize JSON content from given JSON content String.
     */
    public static <T> T parse(String content, Class<T> valueType) throws IOException {
        return mapper.readValue(content, valueType);
    }
}

编写一简单POJO测试类:

import java.util.Date;
public class Hero {
    public static void main(String[] args) throws Exception {
        System.out.println(JsonUtil.generate(new Hero("Jason", new Date())));
    }
    private String name;
    private Date birthday;
    public Hero() {
    }
    public Hero(String name, Date birthday) {
        this.name = name;
        this.birthday = birthday;
    }
    public String getName() {
        return name;
    }
    public Date getBirthday() {
        return birthday;
    }
}

运行后输出结果如下:

{"name":"Jason","birthday":1540909420353}

可以看到日期转换为长整型。

ObjectMapper默认序列化配置启用了SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,日期将转换为Timestamp。

可查看如下源码:

public ObjectMapper(JsonFactory jf, DefaultSerializerProvider sp, DefaultDeserializationContext dc) {
...
    BaseSettings base = DEFAULT_BASE.withClassIntrospector(defaultClassIntrospector());
    _configOverrides = new ConfigOverrides();
    _serializationConfig = new SerializationConfig(base, _subtypeResolver, mixins, rootNames, _configOverrides);
    ...
}
public SerializationConfig(BaseSettings base, SubtypeResolver str, SimpleMixInResolver mixins, RootNameLookup rootNames,
        ConfigOverrides configOverrides)
{
    super(base, str, mixins, rootNames, configOverrides);
    _serFeatures = collectFeatureDefaults(SerializationFeature.class);
    _filterProvider = null;
    _defaultPrettyPrinter = DEFAULT_PRETTY_PRINTER;
    _generatorFeatures = 0;
    _generatorFeaturesToChange = 0;
    _formatWriteFeatures = 0;
    _formatWriteFeaturesToChange = 0;
}

默认情况下,Date类型序列化将调用DateSerializer的_timestamp 方法:

/**
 * For efficiency, we will serialize Dates as longs, instead of
 * potentially more readable Strings.
 */
@JacksonStdImpl
@SuppressWarnings("serial")
public class DateSerializer extends DateTimeSerializerBase<Date> {
    ...
    @Override
    protected long _timestamp(Date value) {
        return (value == null) ? 0L : value.getTime();
    }
    @Override
    public void serialize(Date value, JsonGenerator g, SerializerProvider provider) throws IOException {
        if (_asTimestamp(provider)) {
            g.writeNumber(_timestamp(value));
            return;
        }
        _serializeAsString(value, g, provider);
    }
}

DateTimeSerializerBase的_asTimestamp方法:

protected boolean _asTimestamp(SerializerProvider serializers)
{
    if (_useTimestamp != null) {
        return _useTimestamp.booleanValue();
    }
    if (_customFormat == null) {
        if (serializers != null) {
            return serializers.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        }
        // 12-Jun-2014, tatu: Is it legal not to have provider? Was NPE:ing earlier so leave a check
        throw new IllegalArgumentException("Null SerializerProvider passed for "+handledType().getName());
    }
    return false;
}

禁用WRITE_DATES_AS_TIMESTAMPS

若要将日期序列化为字符串,可禁用SerializationFeature.WRITE_DATES_AS_TIMESTAMPS:

mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

这时序列化将调用StdDateFormat的format()方法,使用ISO-8601兼容格式"yyyy-MM-dd'T'HH:mm:ss.SSSZ",输出内容如下:

{"name":"Jason","birthday":"2018-10-31T03:07:34.485+0000"}

StdDateFormat反序列化支持ISO-8601兼容格式和RFC-1123("EEE, dd MMM yyyy HH:mm:ss zzz")格式。

@JsonFormat

使用@JsonFormat注解,代替全局设置,是一种更灵活的方法:

@JsonFormat(shape = JsonFormat.Shape.STRING)
private Date birthday;

还可以定义pattern:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date birthday;

当自定义pattern后,将创建新的SimpleDateFormat实例来序列化日期,参见DateTimeSerializerBase的createContextual()方法:

public JsonSerializer<?> createContextual(SerializerProvider serializers, BeanProperty property) throws JsonMappingException
{
  ...
  if (format.hasPattern()) {
      final Locale loc = format.hasLocale() ? format.getLocale() : serializers.getLocale();
      SimpleDateFormat df = new SimpleDateFormat(format.getPattern(), loc);
      TimeZone tz = format.hasTimeZone() ? format.getTimeZone() : serializers.getTimeZone();
      df.setTimeZone(tz);
      return withFormat(Boolean.FALSE, df);
  }
  ...
}

Spring Boot与Jackson ObjectMapper

Spring Boot使用HttpMessageConverters处理HTTP交换中的内容转换。

当classpath中存在Jackson时,Jackson2ObjectMapperBuilder将是默认的Converter,源码请查看HttpMessageConverters和WebMvcConfigurationSupport:

HttpMessageConverters

private List<HttpMessageConverter<?>> getDefaultConverters() {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    if (ClassUtils.isPresent("org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport", null)) {
        converters.addAll(new WebMvcConfigurationSupport() {
            public List<HttpMessageConverter<?>> defaultMessageConverters() {
                return super.getMessageConverters();
            }
        }.defaultMessageConverters());
    }
    else {
        converters.addAll(new RestTemplate().getMessageConverters());
    }
    reorderXmlConvertersToEnd(converters);
    return converters;
}

WebMvcConfigurationSupport

protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
    StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
    stringHttpMessageConverter.setWriteAcceptCharset(false);  // see SPR-7316
    messageConverters.add(new ByteArrayHttpMessageConverter());
    messageConverters.add(stringHttpMessageConverter);
    messageConverters.add(new ResourceHttpMessageConverter());
    messageConverters.add(new ResourceRegionHttpMessageConverter());
    messageConverters.add(new SourceHttpMessageConverter<>());
    messageConverters.add(new AllEncompassingFormHttpMessageConverter());
    ...
    if (jackson2Present) {
        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
        if (this.applicationContext != null) {
            builder.applicationContext(this.applicationContext);
        }
        messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build()));
    }
    ...
}

默认,Jackson2ObjectMapperBuilder将创建ObjectMapper实例:

Jackson2ObjectMapperBuilder

public <T extends ObjectMapper> T build() {
    ObjectMapper mapper;
    if (this.createXmlMapper) {
        mapper = (this.defaultUseWrapper != null ?
                new XmlObjectMapperInitializer().create(this.defaultUseWrapper) :
                new XmlObjectMapperInitializer().create());
    }
    else {
        mapper = (this.factory != null ? new ObjectMapper(this.factory) : new ObjectMapper());
    }
    configure(mapper);
    return (T) mapper;
}

ObjectMapper以下属性被禁用:

  • MapperFeature.DEFAULT_VIEW_INCLUSION
  • DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
  • SerializationFeature.WRITE_DATES_AS_TIMESTAMPS

Jackson2ObjectMapperBuilder

private void customizeDefaultFeatures(ObjectMapper objectMapper) {
    if (!this.features.containsKey(MapperFeature.DEFAULT_VIEW_INCLUSION)) {
        configureFeature(objectMapper, MapperFeature.DEFAULT_VIEW_INCLUSION, false);
    }
    if (!this.features.containsKey(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
        configureFeature(objectMapper, DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
}

JacksonAutoConfiguration

static {
    Map<Object, Boolean> featureDefaults = new HashMap<>();
    featureDefaults.put(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    FEATURE_DEFAULTS = Collections.unmodifiableMap(featureDefaults);
}

自定义Jackson ObjectMapper配置

针对ObjectMapper的六种Feature,Spring Boot都提供了相应的配置,列表如下:

Feature(Enum)Spring Boot PropertyValues
com.fasterxml.jackson.databind.DeserializationFeaturespring.jackson.deserialization.feature_nametrue, false
com.fasterxml.jackson.core.JsonGenerator.Featurespring.jackson.generator.feature_nametrue, false
com.fasterxml.jackson.databind.MapperFeaturespring.jackson.mapper.feature_nametrue, false
com.fasterxml.jackson.core.JsonParser.Featurespring.jackson.parser.feature_nametrue, false
com.fasterxml.jackson.databind.SerializationFeaturespring.jackson.serialization.feature_nametrue, false
com.fasterxml.jackson.annotation.JsonInclude.Includespring.jackson.default-property-inclusionalways, non_null, non_absent, non_default, non_empty

例如,为启用美化打印,设置spring.jackson.serialization.indent_output=true,相当于启用SerializationFeature.INDENT_OUTPUT,配置中忽略feature_name大小写。

其他的Jackson配置属性:

  • spring.jackson.date-format= # Date format string or a fully-qualified date format class name. For instance, yyyy-MM-dd HH:mm:ss.
  • spring.jackson.joda-date-time-format= # Joda date time format string. If not configured, "date-format" is used as a fallback if it is configured with a format string.
  • spring.jackson.locale= # Locale used for formatting.
  • spring.jackson.property-naming-strategy= # One of the constants on Jackson's PropertyNamingStrategy. Can also be a fully-qualified class name of a PropertyNamingStrategy subclass.
  • spring.jackson.time-zone= # Time zone used when formatting dates. For instance, "America/Los_Angeles" or "GMT+10".

其中spring.jackson.date-format默认值为com.fasterxml.jackson.databind.util.StdDateFormat。

@DateTimeFormat和@JsonFormat

在REST编程中,当提交application/json的POST/PUT请求时,JSON会通过Jackson进行转换。

当提交GET请求时,如参数中包含日期,后台代码需要使用注解@DateTimeFormat:

@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date startDate;

两者可以同时使用:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date startDate;

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot实现联表查询的代码详解

    SpringBoot实现联表查询的代码详解

    这篇文章主要介绍了SpringBoot中如何实现联表查询,文中通过代码示例和图文结合的方式讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-05-05
  • Spring框架中@AliasFor注解详细说明

    Spring框架中@AliasFor注解详细说明

    这篇文章主要给大家介绍了关于Spring框架中@AliasFor注解详细说明的相关资料,@AliasFor是Spring Framework中的一个注解,它用于指定注解属性之间的别名关系,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-02-02
  • java微信开发API第一步 服务器接入

    java微信开发API第一步 服务器接入

    这篇文章主要为大家分享了java微信开发API的第一步操作服务器接入,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • SpringBoot/SpringCloud使用jasypt-spring-boot实现配置文件加密

    SpringBoot/SpringCloud使用jasypt-spring-boot实现配置文件加密

    在现代软件开发和部署中,应用程序的配置文件常常包含敏感信息,本文将详细介绍如何使用 jasypt-spring-boot 库,在 Spring Boot 和 Spring Cloud 项目中实现配置文件内容的加密与解密,需要的朋友可以参考下
    2026-05-05
  • Spring Cloud Hystrix入门和Hystrix命令原理分析

    Spring Cloud Hystrix入门和Hystrix命令原理分析

    这篇文章主要介绍了Spring Cloud Hystrix入门和Hystrix命令原理分析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • 微服务Spring Cloud Alibaba 的介绍及主要功能详解

    微服务Spring Cloud Alibaba 的介绍及主要功能详解

    Spring Cloud 是一个通用的微服务框架,适合于多种环境下的开发,而 Spring Cloud Alibaba 则是为阿里巴巴技术栈量身定制的解决方案,本文给大家介绍Spring Cloud Alibaba 的介绍及主要功能,感兴趣的朋友跟随小编一起看看吧
    2024-08-08
  • Java.lang.OutOfMemoryError: GC overhead limit exceeded错误的解决

    Java.lang.OutOfMemoryError: GC overhead limit

    本文主要介绍了Java.lang.OutOfMemoryError: GC overhead limit exceeded错误的解决,错误是由于堆空间不足导致GC频繁运行,从而引起的,下面就来介绍一下解决方法
    2025-03-03
  • 散列算法与散列码(实例讲解)

    散列算法与散列码(实例讲解)

    下面小编就为大家带来一篇散列算法与散列码(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • 基于java使用JavaMail发送邮件

    基于java使用JavaMail发送邮件

    这篇文章主要介绍了基于java使用JavaMail发送邮件 ,非常具有实用价值,需要的朋友可以参考下。
    2016-12-12
  • Java中的事件处理机制详解

    Java中的事件处理机制详解

    这篇文章主要介绍了Java中的事件处理机制详解,Java事件处理是采取"委派事件模型",当事件发生时,产生事件的对象,会把此"信息"传递给"事件的监听者"处理,这里所说的"信息"实际上就是java.awt.event事件类库里某个类创建对象,需要的朋友可以参考下
    2023-09-09

最新评论