MappingJackson2HttpMessageConverter(undone)

@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

猜你喜欢

转载自blog.csdn.net/qq_35070673/article/details/78837151
今日推荐