Spring boot + Jackson + LocalDateTime:日期解析不正确

Posted

技术标签:

【中文标题】Spring boot + Jackson + LocalDateTime:日期解析不正确【英文标题】:Spring boot + Jackson + LocalDateTime: Date not parsed correctly 【发布时间】:2018-04-06 20:13:42 【问题描述】:

我的实体类中有一个 LocalDateTime 属性,但是当它被序列化时,我看不到预期的格式。

这是课程:

public class MyEntity 

     private Integer id;
     @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
     private LocalDateTime changeDate;

     // getters and setters


杰克逊是这样格式化的:


    "id": 56, 
    "changeDate": 
        "hour":14, "minute":19, 
        "nano":797000000, 
        "second":7, 
        "dayOfMonth":24, 
        "dayOfWeek":"TUESDAY", 
        "dayOfYear":297, "month":"OCTOBER", 
        "monthValue":10, 
        "year":2017,
        "chronology":  
             "id":"ISO",
             "calendarType":"iso8601"
        
    

请注意,我在 pom 中添加了以下依赖项:

<dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>

我不包括该版本,因为 Spring Boot 会处理这个问题。 顺便说一句,我使用的是 spring boot 1.5.2.RELEASE。

我还在 application.properties 中包含了以下属性:

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

知道为什么日期的格式不是这样,而是使用我提供的模式吗?

【问题讨论】:

如果你使用spring.jackson.mapper.WRITE_DATES_AS_TIMESTAMPS会发生什么? 这通常发生在JavaTimeModule 未在ObjectMapper 中注册时。我以为spring会自动检测并注册模块,但它似乎没有这样做。 不直接相关,但在格式中您附加了文字Z,实际上是UTC designator。将 LocalDateTime 视为 UTC 可能是错误的,具体取决于您对它的处理方式。也许将其更改为OffsetDateTimeInstant 更准确。 正如@Hugo 所说,该模块没有被注册,因此Jackson 只将其视为POJO,使用LocalDateTime 的“getter”对其进行序列化。我不知道 Spring Boot 是否应该自动检测模块(技术上可以通过 SPI 元数据来实现);如果没有找到支持这一点的文档,我不会指望这一点。 WRITE_DATES_AS_TIMESTAMPS = false 尝试删除空格 【参考方案1】:

刚刚检查了我的项目。我的反序列化器:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;
import java.time.LocalDateTime;

import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;

public class JsonDateTimeDeserializer extends JsonDeserializer<LocalDateTime> 

private static final String NULL_VALUE = "null";

@Override
public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException 
    ObjectCodec oc = jp.getCodec();
    JsonNode node = oc.readTree(jp);
    String dateString = node.textValue();

    LocalDateTime dateTime = null;
    if (!NULL_VALUE.equals(dateString)) 
        dateTime = LocalDateTime.parse(dateString, ISO_LOCAL_DATE_TIME);
    
    return dateTime;


我的序列化器:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.LocalDateTime;

import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;

public class JsonLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> 
@Override
public void serialize(LocalDateTime dateTime, JsonGenerator generator, SerializerProvider provider)
        throws IOException 

    String dateTimeString = dateTime.format(ISO_LOCAL_DATE_TIME);
    generator.writeString(dateTimeString);


您需要设置自己的格式化程序。

在我的 RestConfig 我有:

@Configuration
@ComponentScan(value = "ru.outofrange.controller")
public class RestConfig extends RepositoryRestConfigurerAdapter 

@Override
public void configureJacksonObjectMapper(ObjectMapper objectMapper) 
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

    registerSerializerDeserializer(objectMapper);


private void registerSerializerDeserializer(ObjectMapper objectMapper) 
    SimpleModule module = new SimpleModule();

    module.addSerializer(LocalDateTime.class, new JsonLocalDateTimeSerializer());
    module.addDeserializer(LocalDateTime.class, new JsonDateTimeDeserializer());

    objectMapper.registerModule(module);



【讨论】:

【参考方案2】:

我尝试重现您的示例,效果很好。

MyEntity

public class MyEntity 
    private Integer id;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")
    private LocalDateTime time;

    public MyEntity(Integer id, LocalDateTime time) 
        this.id = id;
        this.time = time;
    

    public Integer getId() 
        return id;
    

    public void setId(Integer id) 
        this.id = id;
    

    public LocalDateTime getTime() 
        return time;
    

    public void setTime(LocalDateTime time) 
        this.time = time;
    

LocalDateTimeApplication

@SpringBootApplication
public class LocalDateTimeApplication 

    public static void main(String[] args) 
        SpringApplication.run(LocalDateTimeApplication.class, args);
    

SomeController

@RestController
@RequestMapping("/SomeController")
public class SomeController 

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<Object> getMyEntity() 
        MyEntity entity = new MyEntity(1, LocalDateTime.now());
        return new ResponseEntity<Object>(entity, HttpStatus.OK);
    

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.so.example</groupId>
    <artifactId>localDateTime</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>localDateTime</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

【讨论】:

以上是关于Spring boot + Jackson + LocalDateTime:日期解析不正确的主要内容,如果未能解决你的问题,请参考以下文章

Jackson 在我的 Spring Boot 应用程序中忽略了 spring.jackson.properties

如何在 Spring Boot 1.4 中自定义 Jackson

Spring Boot没有使用Jackson Kotlin插件

Spring Boot 未使用 Jackson Kotlin 插件

spring-boot用jackson改变json响应结构

Spring boot + Jackson + LocalDateTime:日期解析不正确