MappingJackson2HttpMessageConverter(undone)

本文介绍SpringMVC框架中日期格式化的处理方式及如何使用Jackson进行序列化与反序列化操作。针对不同场景,提供了具体的配置示例,如日期格式化输出设置及将null值序列化为空字符串的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@JsonInclude

  The @JsonInclude annotation is used to exclude properties or fields of a class under certain conditions. This is defined using the JsonInclude.Include enum. This enum contains constants that determine whether or not to exclude the property. The constants are:

  • ALWAYS
  • NON_DEFAULT
  • NON_EMPTY
  • NON_NULL

spring mvc默认是支持yyyy-MM-dd格式的字符串转换为java的java.util.Date.包括spring mvc框架本身和spring mvc支持的jackson.
对于其它格式的日期的字符串与Java的Date对象相互转化,一样可分为两种情况:
A:一种普通请求,前台的日期字符串与后台的Java Date对象转化,此情况,应使用spring mvc本身的内置日期处理.
B:另一种将参数写进请求体里面,使用application/json这样的mediaType发请求,对于此情况,应使用Jackson的反序列化来处理.


// 日期格式化输出
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));

// 将null序列化为空字符串
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            @Override
            public void serialize(Object value, JsonGenerator jg, SerializerProvider sp) throws IOException {
                jg.writeString("");
            }
        });

参考资料

  1. Jackson Annotations for JSON (Part 1): Serialization and
    Deserialization
  2. Jackson Annotations for JSON (Part 2): Serialization
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值