请您多多留言指教(自己搭建springboot2.3.1+mybatisplus3.3.2)
注:以下为搭建项目并查找@JsonFormat注解不生效的几处关键位置
com.fasterxml.jackson.core jackson-core 2.11.0 com.fasterxml.jackson.core jackson-annotations 2.11.0 com.fasterxml.jackson.core jackson-databind 2.11.0 org.codehaus.jackson jackson-mapper-asl 1.9.13
@TableName("t_company") public class Company { @TableId(value = "id", type = IdType.ASSIGN_UUID) private String id; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @TableField(value = "created_at") private Date createdAt; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd") @TableField(value = "updated_at") private Date updatedAt; ...其它属性字段省略 }
# jackson全局配置 jackson: # 全局设置@JsonFormat的格式pattern date-format: yyyy-MM-dd HH:mm:ss # 当地时区 locale: zh # 设置全局时区 time-zone: GMT+8 serialization: #格式化输出 indent_output: true # #忽略无法转换的对象 # fail_on_empty_beans: false # deserialization: # #允许对象忽略json中不存在的属性 # fail_on_unknown_properties: false parser: #允许出现特殊字符和转义符 allow_unquoted_control_chars: true #允许出现单引号 allow_single_quotes: true # #如果加该注解的字段为null,那么就不序列化这个字段了 # default-property-inclusion: NON_EMPTY
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializerProvider; 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; import java.io.IOException; @Configuration public class JacksonConfig { /** * 重新注入ObjectMapper * 注:使用此方式ObjectMapper,application中Jackson自动失效 * * @param builder * @return */ @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); // 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化 // Include.Include.ALWAYS 默认 // Include.NON_DEFAULT 属性为默认值不序列化 // Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量 // Include.NON_NULL 属性为NULL 不序列化 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); // 反序列化时 忽略匹配不到的属性字段 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 允许出现单引号 objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); // 字段保留,将null值转为"" objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer
执行结果:
{
"id": "1",
"name": "测试公司1",
"shortName": "测试公司1",
"code": "test",
"createdAt": "2020-06-28 14:52:39",
"updatedAt": "2020-06-28 14:52:39",
......其它属性省略
}
返回结果展示,并非@JsonFormat注解配置的时间格式。
然而在自己搭建springboot+mybatisplus框架中@JsonFormat注解未生效,让自己很纠结,终于找到问题所在!
原因(大马虎来了):类未实现序列化Serializable,导致注解失效
解决方案(亲测已注解正常):
@TableName("t_company") public class Company implements Serializable { private static final long serialVersionUID = -4294461808433313537L; ... 其它代码省略 }
再次执行结果:
{
"id": "1",
"name": "测试公司1",
"shortName": "测试公司1",
"code": "test",
"createdAt": "2020-06-28 14:52:39",
"updatedAt": "2020-06-28",
......其它属性省略
}
注:以上为关键的四处位置,请细心检查(一般情况下只要按照上面配置好,@JsonFormat注解会生效)。